Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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();
Posted
Comments
DamithSL 17-Nov-14 10:57am    
any exceptions? have you debug and check whether your it come to update section of your code when you run the application and have you check the values are correct or not?
Computer Wiz99 17-Nov-14 11:31am    
Yes I have a still nothing.
[no name] 17-Nov-14 11:55am    
After you updated the data, have you tried Databinding the gridview again to refresh the data?
Also, is it updating the database, just not showing the updates to the user or nothing happends on the database?
Computer Wiz99 17-Nov-14 11:59am    
Nothing happens to the database. I checked to make sure but nothing. I also refreshed the GridView to see if it did update but nothing. Could it be my population with the Select link from the GridView?
[no name] 17-Nov-14 12:03pm    
Then go and debug the Submit_Click method until you find what the issue is.

1 solution

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!!
 
Share this answer
 
Comments
Arkadeep De 19-Nov-14 2:43am    
You most welcome...and its not "she"..its "he".... :D

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