Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All. I have page in asp.net Website. My database has table with 3 columns.
Id(int) , title(varchar), state_p(int). default value of state_p = 0 .

I Have GridView with Checkbox within it

I want for each row in Grid View rows when checked of CheckBox1 is true then return id of rows in DropDownList1. This code done it successfully:

C#
foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
            {
                string data;
                data = gvr.Cells[0].Text;
                DropDownList1.Items.Add(data);
            }
        }


The problem is that I want to update state_p columns in my database to “1” WHERE id= '" + DropDownList1.Items + "'.
How can I do that. Please help me.

What I have tried:

how to update column of database where id of column is equal to DropDownList1.Items
Posted
Updated 23-Jun-16 7:02am
Comments
Vincent Maverick Durano 22-Jun-16 15:07pm    
You would need to iterate the items in dropdownlist. Within the loop you could construct your update query and execute it at once in your database.
Maciej Los 23-Jun-16 2:11am    
What have you tried? Where are you stuck?

1 solution

If you are using dynamic SQL by building it in the application, you can do something like this; however, this will only work setting the state_p to 1. You will need to also put in code to reverse. What if the state_p is one, but needs to be zero (unchecked).

** Use this only to step through to see how the code is building the data for the where clause. To properly execute any SQL should be to build a list of values and pass those values to a method, which executes a stored procedure or function on the database server. **

C#
StringBuilder sql = new StringBuilder();

sql.Append(" UPDATE  <TABLE>");
sql.Append(" SET state_p = 1 ");
sql.Append(" WHERE id IN ("

foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
            {
                string data;
                data = gvr.Cells[0].Text;
				sql.Append(data + ",");
                DropDownList1.Items.Add(data);
            }
        }
		
 string exeSql = sql.ToString().Substring(0, sql.Length - 1) + ")";

//Execute the sql statment by using exeSql;
 
Share this answer
 
v2
Comments
[no name] 23-Jun-16 14:23pm    
I appreciate the response and I also agree, since it is an ASP.NET page. I was just trying to show visually what needs to be done, not necessarily using the code as-is. It seemed to be the best way to get my point across.

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