Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below code is for update the record in excel sheet:-

try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\csharp.net-informations.xlsx;Extended Properties=Excel 12.0 Xml;");
MyConnection.Open();
myCommand.Connection = MyConnection;

sql = (@"Update [Sheet1$] set (candidatefname='" + txtFName.Text + "', candidatemname='" + txtMName.Text + "', candidatelname='" + txtLName.Text + "', dob='" + dtpDOB.Value + "', sex='" + cmbSex.Text + "', currentaddress='" + txtCurrentAddress.Text + "', permanentaddress='" + txtParmanentAddress.Text + "', mobilenumber='" + txtMobile.Text + "', mailid='" + txtMailID.Text + "', maritalstatus='" + cmbMaritalStatus.Text + "', experience='" + txtExperience.Text + "', departmentname='" + cmbDepartment.Text + "') where SrNo='" + cID + "' ");
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


I am getting the error : Syntax error in UPDATE statement.

When I am debugging the code getting all the value perfectly as I want, but in sql object I am getting null value.

So please can anybody tell me what is wrong with this?
Posted
Comments
Pheonyx 10-Jul-14 4:08am    
I would suggest you re-factor that code to use parameters else it becomes susceptible to sql injection attacks.

Update query syntax is wrong - update table set field = newvalue where criteria...
you have used "(" after set .
please remove "(" after set and also remove ")" before where
 
Share this answer
 
Remove "(" & ")" after "set" in the SQL it will work

(@"Update [Sheet1$] set (candidatefname='" + txtFName.Text + "', candidatemname='" + txtMName.Text + "', candidatelname='" + txtLName.Text + "', dob='" + dtpDOB.Value + "', sex='" + cmbSex.Text + "', currentaddress='" + txtCurrentAddress.Text + "', permanentaddress='" + txtParmanentAddress.Text + "', mobilenumber='" + txtMobile.Text + "', mailid='" + txtMailID.Text + "', maritalstatus='" + cmbMaritalStatus.Text + "', experience='" + txtExperience.Text + "', departmentname='" + cmbDepartment.Text + "') where SrNo='" + cID + "' ");
 
Share this answer
 
v2
Comments
DT_2 10-Jul-14 6:12am    
If I want to give path of excel sheet from an string object in connection string(Data Source), how can I do that:

("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\csharp.net-informations.xlsx;Extended Properties=Excel 12.0 Xml;");

I do that like this :

string path = "d:\\csharp.net-informations.xlsx"
("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path.ToString()+";Extended Properties=Excel 12.0 Xml;");

But this is giving me error.

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