将单选按钮值插入SQL Server 2005数据库 [英] Insert radio button values to a SQL Server 2005 Database

查看:76
本文介绍了将单选按钮值插入SQL Server 2005数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在紧急处理一个项目.我需要将一些值从Web表单保存到SQL Server 2005数据库中.

我有两个单选按钮,数据类型的男性和女性是Bit.我想保存1或0的值,具体取决于用户的选择,欢迎提出任何想法和示例代码.

代码

Hi all i am working on a project urgent.I need to save some values from my webform to a SQL Server 2005 database.

I have two radio buttons male and female of datatype is Bit.I would like to save a value of 1 or 0 depending on the selection of the user any ideas and sample code will be welcome.

Code

protected void cmdSubmit_Click(object sender, EventArgs e)
       {
           SqlDataStaff.InsertCommand = INSERT INTO [Staff] ([Firstname], +
                                         "[Lastname], [GenderID]) " +
                                         "VALUES ('" + txtFName.Text + "', " +
                                          "'" + txtLastName.Text + "', " +
                                         "" + rdbgender.SelectedValue + ")";
           SqlDataStaff.Insert();
           txtFName.Text = "";
           txtLastName.Text = "";




谢谢




Thanks

推荐答案



使用单选按钮列表选择性别

在这里,我提供一些示例,以从单选按钮列表控件中获取选定的值

Hi,

Use radio button list for gender selection

Here I''m providing some example to get selected value from radio buttonlist control

<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
     <asp:listitem text="Male" value="1"></asp:listitem>
     <asp:listitem text="Female" value="0"></asp:listitem>
    </asp:radiobuttonlist>



并将以下代码放置在按钮单击事件中



and place below code in button click event

if (RadioButtonList1.SelectedIndex >= 0)
            {
                int gend = int.Parse(RadioButtonList1.SelectedValue);
            }



通过使用gend变量,可以将其保存在数据库中

最好的



By using gend variable you can save it in your database

All the Best


Hello Friend,

 Use Radio ButtonList then you can get selected value of radio button for save.


您可以使用2个单选按钮

1. rad_male和2. rad_female

如果(rad_male.checked)
{
//将0存储在databse中
}
其他

{
数据库中的1个
}

如果您的字段是int类型,则可以在后端使用case语句

前任.如果性别= 0,则男"否则女"以性别结尾的情况
you can use 2 radio button

1. rad_male & 2. rad_female

if(rad_male.checked)
{
//store 0 in databse
}
else

{
1 in database
}

if your field is int type then you can use case statement at the back end

Ex. Case when gender=0then ''Male'' else ''Female'' end as Gender


这篇关于将单选按钮值插入SQL Server 2005数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆