Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have added dropdown columns to gridview. i want yes or no values in those dropdowns.But only first value is getting saved in database.

What I have tried:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           



            if (e.Row.RowType == DataControlRowType.DataRow)

            {
                count = e.Row.Cells.Count;
                //if ((e.Row.Cells[5].Text).Equals("Yes"))
                //int index = 3;
                // if(Header.InnerText!="cri")
                if (!IsPostBack)
                {
                    for (i = 5; i < e.Row.Cells.Count; i++)
                    {
                        DropDownList ddl = new DropDownList();
                        ddl.ID = "dd '" + i + "'" + e.Row.Cells[0].Text;
                        ddl.SelectedIndex = 0;
                        // ddl.Items.Add("Select");

                        //ddl(new ListItem("Text", "Value");
                        ddl.Items.Add("No");
                        ddl.Items.Add("yes");
                        //ddl.DataTextField = "yes";
                        //  ddl.DataValueField = " 1";
                        // ddl.DataTextField = "No";
                        //ddl.DataValueField = "0";
                        // ddl.Items.Add("No");
                        // ddl.AutoPostBack = true;
                        // ddl.SelectedIndexChanged += new System.EventHandler(DDL_SelectedIndexChanged);
                        ddl.DataBind();
                        e.Row.Cells[i].Controls.Add(ddl);
                        ddl.Enabled = true;



                       
                        SqlConnection cnn = new SqlConnection("connection string");
                        string query = " update devopstable set  " + GridView1.HeaderRow.Cells[i].Text + "  = '" + ddl.SelectedItem.Text + "'  where  devopsid =@devopsid";
                        SqlCommand cmd = new SqlCommand(query, cnn);
                        ddl.DataBind();
                        cnn.Open();
cmd.Parameters.AddWithValue("@devopsid", devopsid);
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        // FillGrid(); 







                     
                    }

                }

            }
        }
Posted
Updated 8-Oct-18 0:04am
v3
Comments
F-ES Sitecore 8-Oct-18 5:22am    
There is no "WHERE" clause on your sql statement so you are updating every row in the devopstable table. Also you're not actually inserting anything as there is no "INSERT" command.
Vincent Maverick Durano 9-Oct-18 11:42am    
Your code doesn't make any sense at all. Can you please rethink your approach and tell us what you really want to do?

1 solution

Three things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And on a website? That's just irresponsible.

2) Hardcoded connections strings on a website? That's not a good idea at all ...

3) You did notice that you only update the DB when you aren't doing a Postback, didn't you? I.e. you only update the DB when you first load the page, not after the user has changed it ...
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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