下拉值未在数据库中更新 [英] Dropdown values are not getting updated in database

查看:71
本文介绍了下拉值未在数据库中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将下拉列添加到gridview。我想在那些下拉列表中显示是或否。但是只有第一个值被保存在数据库中。



我尝试过:



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







                     
                    }

                }

            }
        }

推荐答案

三件事:

1)永远不要连接字符串构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

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:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

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:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

在网站上?这是不负责任的。



2)网站上的硬编码连接字符串?这根本不是一个好主意...



3)你确实注意到你只是在你没有回发时更新数据库,不是吗? ?即您只在第一次加载页面时更新数据库,而不是在用户更改后更新...

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 ...


这篇关于下拉值未在数据库中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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