Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
            
        }


What I have tried:

I want insert text from selected item from tabble S1 to t1
Posted
Updated 2-May-17 5:25am
v2
Comments
[no name] 2-May-17 10:07am    
Your question is not clear but your code has some issue. Refer below solution for correct code.

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.

C#
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();

             }
        }
 
Share this answer
 
Still first row

http://i.imgur.com/9laX6eM.png
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900