<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td>
<asp:RadioButton ID="rbMale" Text="Male" runat="server" GroupName="Gender" />
<asp:RadioButton ID="rbFemale" Text="Female" runat="server" GroupName="Gender" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
</td>
</tr>
</table>
protected void Submit(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string gender = string.Empty;
if (rbMale.Checked)
{
gender = "M";
}
else if (rbFemale.Checked)
{
gender = "F";
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Persons(Name, Gender) VALUES(@Name, @Gender)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Gender", gender);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
No comments:
Post a Comment