C# .NET - update error in access - Asked By Jahir on 31-May-12 09:08 AM

it is not updating

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\maddy\\nifty\\niftydb.mdb;";

OleDbConnection conn = new OleDbConnection(connectionString);

conn.Open();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = conn;
 if (e.CommandName == "upd")

{



LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;

GridViewRow row = lkbupd.NamingContainer as GridViewRow; 

Label lbid = (Label)row.FindControl("lblid");

TextBox txtitle = (TextBox)row.FindControl("txttitle");

TextBox txpublisher = (TextBox)row.FindControl("txtpublisher");

cmd.CommandText =



"update nifty set Title=" + txtitle.Text + ", Publisher=" + txpublisher.Text + " where id=" + lbid.Text + "";

cmd.ExecuteNonQuery();
GridView1.EditIndex = -1; 


bindgrid();

}

dipa ahuja replied to Jahir on 31-May-12 09:47 AM
Your field looks like of varchar datatype but you have not included the single qoutes into them

"update nifty set Title='" + txtitle.Text + "', Publisher='" + txpublisher.Text + "' where id=" + lbid.Text + ""; 

Jitendra Faye replied to Jahir on 31-May-12 10:23 AM
This is because of your string concatenation problem, change your code like this-

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\maddy\\nifty\\niftydb.mdb;";

OleDbConnection conn = new OleDbConnection(connectionString);

conn.Open();

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
 if (e.CommandName == "upd")

{

 

 

LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;

GridViewRow row = lkbupd.NamingContainer as GridViewRow;

Label lbid = (Label)row.FindControl("lblid");

TextBox txtitle = (TextBox)row.FindControl("txttitle");

TextBox txpublisher = (TextBox)row.FindControl("txtpublisher");

cmd.CommandText =

 

 

"update nifty set Title='" + txtitle.Text + "', Publisher='" + txpublisher.Text + "' where id='" + lbid.Text + "'";

cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;

 

bindgrid();

}

kalpana aparnathi replied to Jahir on 31-May-12 03:01 PM
hi,

You need to put single quate for retrieving text data like this:

cmd.CommandText ="update nifty set Title='" + txtitle.Text + "', Publisher='" + txpublisher.Text + '" where id='" + lbid.Text + "'";


Regards,
Goniey N (Mr. G) replied to Jahir on 01-Jun-12 12:01 AM
When you try to update a value which is in string then you have to pass that value in single quote.


so you need to change in your query like below.

update nifty set Title='" + txtitle.Text + "', Publisher='" + txpublisher.Text + "' where id='" + lbid.Text + "'";



Hope this will now wok for you...