Reputation: 26
I can't figure out why the following code is not updating either my GridView nor my MySQL Database. Can anyone offer me some tips as to what I may be doing incorrectly?
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
connection();
GridViewRow row = GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox textName = (TextBox)row.Cells[3].Controls[0];
TextBox textadd = (TextBox)row.Cells[4].Controls[0];
TextBox textc = (TextBox)row.Cells[5].Controls[0];
String query = "update employeeDB set [First Name:]='" + textName.Text + "', [Last Name:]='" + textadd.Text + "', [Email:]='" + textc.Text + "' where id='" + lblID + 1 + "'";
SqlCommand com = new SqlCommand(query, con);
SqlDataReader dr;
dr = com.ExecuteReader();
GridView1.EditIndex = -1;
bind();
}
Here is my bind method as requested:
private void bind()
{
connection();
string query = "select * from employeeDB where [Last Name:] like'" + TextBox1.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Upvotes: 1
Views: 348
Reputation: 727
What are you getting now? Exception or just no error and nothing is happening? Things to check are db connection string-make sure your connection string is pointing to the db you are targeting. And second i would like to point out that the query is open for sql injection attack (something you need to consider - if you are going to use it to production code).Third, what do you have in the bind method? What data source its trying to bind and with what control? From the sample code itself, it looks like no data is being returned from db.
Updated with: And by the way, should the colon be there in your query? See for instance the colon after first name ([First Name:] ) String query = "update employeeDB set [First Name:]='" + textName.Text + "', [Last Name:]='" + textadd.Text + "', [Email:]='" + textc.Text + "' where id='" + lblID + 1 + "'";
Upvotes: 0
Reputation: 84
GridViewRow row = GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox textName = (TextBox)row.Cells[3].Controls[0];
TextBox textadd = (TextBox)row.Cells[4].Controls[0];
TextBox textc = (TextBox)row.Cells[5].Controls[0];
/*are you sure column names are like [First Name:],[Last Name:] and [Email:] in the table*/
/*Syntax for update command should be like this "UPDATE TableName SET ColumnName1=@Parameter1, ColumnName2=@Parameter2 ....
* WHERE ColumnName=@ParameterName"
*/
String query = "update employeeDB set [First Name:]=@FirstName, [Last Name:]=@LastName, [Email:]=@Email where id=@id";
SqlCommand com = new SqlCommand(query, con);
com.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = textName.Text;
com.Parameters.Add("@LastName", SqlDbType.VarChar).Value = textadd.Text;
com.Parameters.Add("@Email", SqlDbType.VarChar).Value = textc.Text;
com.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(lblID.Text) + 1;
con.Open();
com.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bind();
}
Upvotes: 1
Reputation: 1
you should be doing something like this
//Retrieve the table from the session object. DataTable dt = (DataTable)Session["TaskTable"];
//Update the values.
GridViewRow row = TaskGridView.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
Upvotes: 0
Reputation: 6265
Replace
dr = com.ExecuteReader();
with
com.ExecuteNonQuery();
ExecuteReader
is for SELECT
queries.
Also, in real world application you should not build sql string like you do. Use SqlParameter
instead to avoid sql injection and many other errors.
Upvotes: 3