radiobutton存储在数据库中 [英] radiobutton to store in database
本文介绍了radiobutton存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怀疑点击单选按钮时数值是如何存储在数据库中的。
i have doubt when clicking on radio button how the value is storing in the database.
推荐答案
首先选择单选按钮并将其作为参数传递给查询。
First select the radiobutton and pass it as parameter in query..
sting query = insert into emp(name, gender) values ("AAA", genderRadioBtn.SelectedItem.ToString());
示例:我的设计页面是这样的
example: my designing page like this
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="style1">
<tr>
<td colspan="2">
<h3>
Sample Registraion</h3>
</td>
</tr>
<tr>
<td class="style3">
Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Email</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Gendere</td>
<td>
<asp:RadioButtonList ID="rbtGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td align="center" class="style2" colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</td>
</tr>
<tr>
<td align="center" class="style2" colspan="2">
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
In这个性别选项我使用单选按钮...
这个性别值我想存储在数据库中....为此我使用此编码将此值保存在数据库中...
i在按钮控制中写下这段代码:
In this gender option i use radio button...
this gender value i wanna store in database....for that i use this coding for save this value in database...
i wrote this code in button control:
protected void Button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["srinivasdb"].ConnectionString;
SqlConnection cn = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into GenderSample values(@Name,@Email,@Gender)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@gender", rbtGender.SelectedValue);
if (cn.State == ConnectionState.Closed)
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
lblmsg.Text = "Data entered successfully!!!";
}
如果你觉得我的回答是有用的你编程..标记...作为接受解决方案...
谢谢你:)快乐编码:)
If u feel my answer is useful ur programming ..mark this... as accept solution...
thank u:) Happy coding:)
protected void Button1_Click(object sender, ImageClickEventArgs e)
{
string a="";
if (RadioButton1.Checked == true)
{
a = RadioButton1.Text;
}
else if (RadioButton2.Checked == true)
{
a = RadioButton2.Text;
}
SqlConnection sqlconn = new SqlConnection("server=.\\sqlexpress;database=db;integrated security=true");
SqlCommand sqlcomm = new SqlCommand("insert into reg(gender) values(@gender)", sqlconn);
sqlcomm.Parameters.Add("@gender", SqlDbType.VarChar, 50).Value = a.ToString();
sqlconn.Open();
sqlcomm.ExecuteNonQuery();
sqlconn.close();
}
这篇关于radiobutton存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文