Home > Net >  C#.NET in the gridview SQL update statement to update failed
C#.NET in the gridview SQL update statement to update failed

Time:10-09

I want to do is click on the GRIDVIEW (the content of the code is loaded) UPDATE function button and UPDATE relevant information, for some reason, it is too much trouble), can be used to write their own code instead of automatically generated UPDATE statement,
But writing your own UPDATE statement appear problem, UPDATE information failure, can normal use in SQLrSERVER so I guess is to call the GRIDVIEW there was a problem in the data, but I tried many methods can't use don't know where the problem is hope to help me see predecessors

Post a few screenshot and code below
Figure 1 is normal use in the SQL code
Figure 2 is the breakpoint follow up with relevant variables as null
Figure 3 is the front

Posted on the back-end code below (screenshot below may look better)
using System; Using System. Collections. Generic; Using System. The Data; Using System. The Data. SqlClient. Using System. Linq; Using System. The Web; Using System. Web. UI. Using System. Web. UI. WebControls; Public partial class HTML_ScoreManage: System. Web. UI. Page {SqlConnection sqlcon; SqlCommand sqlcom; String strCon="Data Source=DESKTOP - 79153 ub; Initial Catalog=db_StudentManage; Integrated Security=True "; Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {the bind (); }} protected void GridView1_RowEditing (object sender, GridViewEditEventArgs e) {GridView1. EditIndex=e.N ewEditIndex; The bind (); } protected void


GridView1_RowUpdating (object sender,
Function the GridViewUpdateEventArgs e)//problems
{//screenshot may see more clearly the
Sqlcon=new SqlConnection (strCon);
String SQLSTR="update tb_Choosec001 set SName='" + GridView1. Rows [" e.R owIndex]. Cells [1]. The Text. The ToString (). The Trim () +"', Score='" + GridView1. Rows [" e.R owIndex]. Cells [2]. The Text. The ToString (). The Trim () + "' where SId='" + GridView1. DataKeys [" e.R owIndex]. Value. The ToString () + "' ";
//the above several GRIDVIEW1 data calls for null values in a table
Sqlcom=new SqlCommand (SQLSTR sqlcon); Sqlcon. The Open ();
Sqlcom. ExecuteNonQuery ();
Sqlcon. Close ();
GridView1. EditIndex=1;
The bind ();
}



//cancel


Protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e) {GridView1. EditIndex=1; The bind (); } public void the bind () {string SQLSTR="select * from tb_Choosec001"; Sqlcon=new SqlConnection (strCon); SqlDataAdapter myda=new SqlDataAdapter (SQLSTR sqlcon); The DataSet myds=new DataSet (); Sqlcon. The Open (); Myda. The Fill (myds, "tb_Choosec001"); GridView1. The DataSource=myds; GridView1. DataKeyNames=new string [] {} "SId";//primary key GridView1. DataBind (); Sqlcon. Close (); }}

CodePudding user response:

Figure 2 is the method of error should be update SQL statement calling the gridview errors of the data in the method

CodePudding user response:

But don't understand why is an empty value tried many ways

CodePudding user response:

Did you at the front desk and DataKeyNames="Sid"

CodePudding user response:

The back-end code in the bind () in it

CodePudding user response:

To ensure that all values are taken and then update statement without error, can, and then specify the datasource in databind ()

CodePudding user response:

reference 5 floor XBodhi. Response:
make sure that all values to then update statement without error, can, and then specify the datasource in the databind ()

You haven't been able to take is to value to but I don't know what went wrong can you help me take the value of the form in the gridview statement what's the problem
  • Related