Storing radio button and checkbox value in database

Posted by Shivamthinks under ASP.NET on 4/4/2012 | Points: 10 | Views : 5147 | Status : [Member] | Replies : 1
Hi,

My requirement is as follows:

I have 2 radio buttons in aspx page say male and female. i have to store either one of the value of the radio button in the database(Sqlserver).
What is the code for it? I need the whole code. Please help.


I have 2checkboxes in aspx page say apple, mango. i have to store either one or both of the values on checking of the checkbox in the database(Sqlserver).
What is the code for it? I need the whole code. Please help.




Responses

Posted by: Self-Innovator on: 4/5/2012 [Member] Bronze | Points: 25

Up
0
Down
try lik this
Design Page
<table>
<tr>
<td>
<asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Male" Value="1"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" Width="154px" AutoPostBack="True" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem>Select Country</asp:ListItem>
</td>
</tr>
</table>

Code Behind

On Button Click Save Event

Save_Click()
{
string pGender=rblGender.SelectedItem.Text;
String pCountry= ddlCountry.SelectedItem.Text;
SqlConnection cnn=new SqlConnection(Conn);
SqlCommand cmd=New SqlCommand("spInsert",cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pGender", pGender);
cmd.Parameters.AddWithValue("@pCountry", pCountry);
SqlDataAdapter ada=new SqlDataAdapter(cmd);
Dataset ds = new DataSet()
ada.Fill(ds);
ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('Record inserted Successfully')</script>");
}
Method for Binding Countries for ddlCountry
public DataSet GetCountries()
{
SQlconnection cnn = new SqlConnection(Conn);
Sqlcommand cmd = new SqlCommand("sp_GetCountryList", cnn);
cmd.CommandType = CommandType.StoredProcedure;
sqlDataadapter ada = new SqlDataAdapter(cmd);
Dataset ds = new DataSet();
ada.Fill(ds);
return ds;
}

Page Load()
{
If(!ispostback)
{
GetCountries()//here Bind the Countries
}
}

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Shivamthinks, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response