[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");
}

 

Posted 28 days ago

Hi KatieNgoc,

Refer below sample.

Make sure in your datatable is having record.

HTML

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

Default.aspx

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

Logout.aspx

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

Namespaces

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

Login.aspx.cs

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

Defauls.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);
        if (dt.Rows.Count > 0)
        {
            chckStatus.Checked = Convert.ToBoolean(dt.Rows[0]["Status"].ToString()!= "true") ? true : false;
        }
    }
}

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");
}

 


Posted 28 days ago

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30456: 'Save' is not a member of 'default_aspx'.

Source Error:

 
Line 2:  
Line 3:  <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
Line 4:  <asp:CheckBox Text="Remember Me" runat="server" OnCheckedChanged="Save" ID="chckStatus"
Line 5:      AutoPostBack="true" /><br />
Line 6:  <asp:Button ID="Button1" Text="NextPage" runat="server" OnClick="NextPage" />


Source File: C:\Users\Katie Le\source\repos\RememberAgreeCheckBox\RememberAgreeCheckBox\Default.aspx    Line: 4 

 

I got the error as the above