从绑定radiobuttonlist插入DB文本 [英] Insert DB text from binding radiobuttonlist

查看:54
本文介绍了从绑定radiobuttonlist插入DB文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿。

我从RadioButtonList中绑定所选项目时插入到Text中有一些奇怪的问题我从SQL中获取了所有未选中的第一行名称。



这是我的代码:





Hey.
I have weird problem to insert into Text from binding selected item from RadioButtonList i get allways first row name from SQL not selected.

here is my code:


 if (!IsPostBack) 

          {

                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"server=localhost; database=PS_User; trusted_connection=true;";

                SqlCommand cmd = new SqlCommand(@"Select * from S1 WHERE PART =@1 AND PART IS NOT NULL Order by point", con);
                cmd.Parameters.AddWithValue("@0", 0);
                cmd.Parameters.AddWithValue("@1", 1);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataTable dt = new DataTable();
                da.Fill(dt);

                RadioButtonList1.DataTextField = "Name";
                RadioButtonList1.DataValueField = "Point";
                RadioButtonList1.DataSource = dt;
                RadioButtonList1.DataBind();


          }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"server=localhost; database=PS_User; trusted_connection=true;";

            string Name = RadioButtonList1.SelectedItem.Text.ToString();
            string queryString = @"

insert into t1 (Name,Point) values (@Name,@Point)

";

                SqlCommand cmd = new SqlCommand(queryString, con);
                cmd.Parameters.AddWithValue("@Point", Label1.Text);
                cmd.Parameters.AddWithValue("@Name", Name);

                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
            
        }





我的尝试:



我希望从tabble S1到t1的选定项目插入文本



What I have tried:

I want insert text from selected item from tabble S1 to t1

推荐答案

1。你不需要在这里填充SqlDataAdapter时运行 ExecuteNonQuery

2.你的按钮事件为什么你关闭并重新打开sql连接。而是使用使用块来创建连接对象,它会处理它。



1. You dont need to run ExecuteNonQuery as you are populating SqlDataAdapter here.
2. Your button event why are you closing and reopening sql connection. instead use using block to create connection object it will take care of it.

if (!IsPostBack) 
            {
                using(SqlConnection con = new SqlConnection(@"server=localhost; database=PS_User; trusted_connection=true;"))
                {
                    con.open();

                    SqlCommand cmd = new SqlCommand(@"Select * from S1 WHERE PART =@1 AND PART IS NOT NULL Order by point", con);
                    cmd.Parameters.AddWithValue("@0", 0);
                    cmd.Parameters.AddWithValue("@1", 1);
 
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
 
                    RadioButtonList1.DataTextField = "Name";
                    RadioButtonList1.DataValueField = "Point";
                    RadioButtonList1.DataSource = ds.Table[0];
                    RadioButtonList1.DataBind();
                }
          }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
             using(SqlConnection con = new SqlConnection(@"server=localhost; database=PS_User; trusted_connection=true;"))
             {
                 con.open();

                 string Name = RadioButtonList1.SelectedItem.Text.ToString();
                 string queryString = @"insert into t1 (Name,Point) values (@Name,@Point)";
 
                 SqlCommand cmd = new SqlCommand(queryString, con);
                 cmd.Parameters.AddWithValue("@Point", Label1.Text);
                 cmd.Parameters.AddWithValue("@Name", Name);
                 cmd.ExecuteNonQuery();

             }
        }


仍然是第一行



http://i.imgur.com/9laX6eM.png
Still first row

http://i.imgur.com/9laX6eM.png


这篇关于从绑定radiobuttonlist插入DB文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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