I have 2 radio button list boxes, each radiolistbox has 3 radio text options to select, I will select 1 radio text for each. Next time, when i login again, (with session) it will show the selected radio text that I had selected yesterday.
When I try to run the codes as above it shown an error
System.IndexOutOfRangeException: 'There is no row at position 0.'
Do you have any idea to fix this?
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<br />
<br />
<br />
Test 1
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatLayout="Table" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="*Answer 1*" Value="1" />
<asp:ListItem Text="*Answer 2*" Value="2" />
<asp:ListItem Text="*Answer 3*" Value="3" />
</asp:RadioButtonList>
<br />
<br />
<br />
Test2
<asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="1" />
<asp:ListItem Text="*Answer 2*" Value="2" />
<asp:ListItem Text="*Answer 3*" Value="3" />
</asp:RadioButtonList>
<asp:Button Text="NextPage" runat="server" OnClick="NextPage"/>
</asp:Content>
namespace Radiolistbutton
{
public partial class _Default : Page
{
public string constr;
protected void Page_Load(object sender, EventArgs e)
{
constr = ConfigurationManager.ConnectionStrings["Citistaff"].ConnectionString;
if (!this.IsPostBack)
{
// string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT Status1, Status2 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);
// con.Close();
// chckStatus.Checked = Convert.ToBoolean(dt.Rows[0]["Status"]);
RadioButtonList1.SelectedValue = dt.Rows[0]["Status1"].ToString();
RadioButtonList2.SelectedValue = dt.Rows[0]["Status2"].ToString();
}
}
protected void Save(object sender, EventArgs e)
{
string status1 = RadioButtonList1.SelectedValue;
string status2 = RadioButtonList2.SelectedValue;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("UPDATE tblStatusCheck SET Status1 = @Status1, Status2 = @Status2 WHERE Name = @Name", con);
cmd.Parameters.AddWithValue("@Status1", status1);
cmd.Parameters.AddWithValue("@Status2", status2);
cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void NextPage(object sender, EventArgs e)
{
Response.Redirect("Logout.aspx");
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}