[Solved] System.IndexOutOfRangeException: There is no row at position 0

Last Reply 27 days ago By pandeyism

Posted 28 days ago

Check box remember check mark

I got an error System.IndexOutOfRangeException: 'There is no row at position 0.'

CREATE TABLE tblStatusCheck(Name VARCHAR(MAX), [Status] BIT)

Login.aspx

Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button Text="Login" runat="server" OnClick="Logins" />

Default.aspx

<asp:CheckBox Text="Remember Me" runat="server" OnCheckedChanged="Save" ID="chckStatus" AutoPostBack="true" /><br />
<asp:Button Text="NextPage" runat="server"  OnClick="NextPage"/>

Logout.aspx

<asp:HyperLink NavigateUrl="~/Login.aspx" runat="server" Text="Logout" />

Namespaces

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

Login.aspx.cs

protected void Logins(object sender, EventArgs e)
{
    Session["Name"]=txtName.Text;
    Response.Redirect("Default.aspx");
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT Status FROM tblStatusCheck WHERE @Name = @Name", con);
        cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        chckStatus.Checked = Convert.ToBoolean(dt.Rows[0]["Status"]);
    }
}
 
protected void Save(object sender, EventArgs e)
{
    bool status = chckStatus.Checked;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("UPDATE tblStatusCheck SET Status = @Status WHERE Name = @Name", con);
    cmd.Parameters.AddWithValue("@Status", status);
    cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}
 
protected void NextPage(object sender, EventArgs e)
{
    Response.Redirect("Logout.aspx");
}

 

You are viewing reply posted by: pandeyism 27 days ago.