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