从GridView中选择行时,更新无效 [英] Update is not working when row is selected from GridView

查看:89
本文介绍了从GridView中选择行时,更新无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单,上面有七个文本框和一个GridView。当用户访问Web表单时,他们可以在GridView的数据库中看到他们的数据。用户可以单击GridView中的Select链接,GridView中的数据将填充到Web表单上的文本框中。这些部分有效但用户看到其中一个或两个文本框中的数字错误。用户可以更改文本框中的数字并更新之前的数据。更新不适用于用户。代码哪里出错?



以下是从GridView上的Select链接填充文本框的代码:

I have a web form that has seven textboxes on it and a GridView. When the user comes to the web form they can see their data that in already in the database in the GridView. The user can click on the Select link in the GridView and the data from the GridView will populate into the textboxes that is on the web form. That parts works but the user sees that one or two of the textboxes has the wrong numbers in it. the user can change the numbers in the textbox and Update the data they had earlier. The Update is not working for the user. Where did the code go wrong?

Here is the code to populate the textboxes from the Select link on the GridView:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            TextBoxUNITID.Text = row.Cells[1].Text;
            TextBoxTNUGSC.Text = row.Cells[2].Text;
            TextBoxTNUGSCD.Text = row.Cells[3].Text;
            TextBoxTTOUG.Text = row.Cells[4].Text;
            TextBoxTNGSC.Text = row.Cells[5].Text;
            TextBoxTNGSCD.Text = row.Cells[6].Text;
            TextBoxTTOG.Text = row.Cells[7].Text;
        }





这是更新代码:



Here is the Update Code:

protected void Submit_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            const string cmdStr = "Select count(*) from Table88 where User_ID= @User_ID";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
            userExist.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 0)
            {

                SqlCommand cmd2 = new SqlCommand("Insert into Table88 (User_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE);Insert into Table99 (User_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@User_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE);", con);

                cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@BTRANSFERS", TextBoxTTOG.Text.Replace(",", ""));
                cmd2.Parameters.AddWithValue("@YEAR", lblYear1.Text);
                cmd2.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
                cmd2.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
                cmd2.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);

                cmd2.ExecuteNonQuery();
            }
            else if (temp == 1)
            {
                SqlCommand cmd3 = new SqlCommand("UPDATE Table88 SET UNITID = @UNITID, ASTUDENTS = @ASTUDENTS, ACOMPLETED = @ACOMPLETED, ATRANSFERS = @ATRANSFERS, BSTUDENTS = @BSTUDENTS, BCOMPLETED = @BCOMPLETED, BTRANSFERS = @BTRANSFERS, YEAR = @YEAR, DATE = @DATE WHERE User_ID = @User_ID;", con);
                cmd3.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@BTRANSFERS", TextBoxTTOG.Text.Replace(",", ""));
                cmd3.Parameters.AddWithValue("@YEAR", lblYear1.Text);
                cmd3.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
                cmd3.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
                cmd3.Parameters.AddWithValue("@User_ID", TextBoxuser_ID.Text);

                cmd3.ExecuteNonQuery();
             }
            con.Close();

推荐答案

我已经解决了我的问题并且代码正常运行。感谢Arkadeep De,他/她让我看看我忘记的事情。再次感谢!!
I have solved my issue and the code is working. Thanks to Arkadeep De, he/she had me to look at something that I was forgetting. Thanks again!!


这篇关于从GridView中选择行时,更新无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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