Hi All, I need some suggestions on following code:
Scenario: Its like a Opinion poll, You have to choose Yes/No and the data will be stored on DB.But I think this code can be optimized,can some body suggest proper way of doing this.
<form id="form1" runat="server">
<div>
<asp:radiobuttonlist id="RadioButtonList1" runat="server">
<asp:ListItem Text="yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:radiobuttonlist>
</div>
<div>
<asp:button id="Button1" runat="server" text="Submit" onclick="Button1_Click" />
</div>
</form>
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButtonList1.Items.FindByValue("1").Selected == true)
{
string source = "Server=localhost;Database=Test;Trusted_Connection=yes";
SqlConnection con = new SqlConnection(source);
con.Open();
SqlCommand cmd = new SqlCommand("proc_select", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows==true)
{
while (dr.Read())
{
int val = (int)dr["yes"];
val++;
update(val);
}
}
}
if (RadioButtonList1.Items.FindByValue("0").Selected == true)
{
string source = "Server=localhost;Database=Test;Trusted_Connection=yes";
SqlConnection con = new SqlConnection(source);
con.Open();
SqlCommand cmd = new SqlCommand("proc_select", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
{
int val = (int)dr["No"];
val++;
updateNo(val);
}
}
}
}
public void update(int val)
{
string source = "Server=localhost;Database=Test;Trusted_Connection=yes";
SqlConnection con = new SqlConnection(source);
SqlCommand cmdupd;
con.Open();
cmdupd = new SqlCommand("proc_Update", con);
cmdupd.Parameters.Add("@val", SqlDbType.NVarChar).Value = val;
cmdupd.CommandType = CommandType.StoredProcedure;
cmdupd.ExecuteNonQuery();
}
UpdateNo()
method will be same as update()
.
I will declare sqlcommand
and source like things globally, so not on that area. Let me know if u want more information.