how do i update data in access database using c# 2010 - c#

OleDbConnection vcon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\SummerJob\DataBase.accdb");
string cmdtxt = "UPDATE Students SET S_Name = ?, S_Surname = ?, S_E-Mail = ? WHERE ID = ?";
OleDbCommand cmd = new OleDbCommand(cmdtxt, vcon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("S_Name", EditName.Text);
cmd.Parameters.AddWithValue("S_Surname", editSurname.Text);
cmd.Parameters.AddWithValue("S_E-Mail", editMail.Text);
vcon.Open();
cmd.ExecuteNonQuery();
vcon.Close();
//i use this code but it says syntax error in update statement

Where is the last parameter ???
ID=?
Not sure, but I will bet that without that parameter your query doesn't look right to the parser.

You need to pass ID? via SqlParameter like this cmd.Parameters.AddWithValue("ID", Id.Text);
Also make sure that the parameters are added in the same order they occur

Related

C# Sql Incorrect syntax error

In the following command I get the exception
Incorrect syntax near 'TPA_Approved_Date'
What am I doing wrong?:
SqlCommand sqlComm = new SqlCommand();
sqlComm = myConnection.CreateCommand();
sqlComm.CommandText = #"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F]
SET TPA_Approver_Code='#Approver_Code'
TPA_Approved_Date ='#Approved_Date'
TPA_Approved_Flag='Y' WHERE
MDM_Invoice_Date_Dim_Key='20150206'";
sqlComm.Parameters.AddWithValue("#Approver_Code", Approver_Code);
sqlComm.Parameters.AddWithValue("#Approved_Date", "2015-05-06 16:24:47.870");
don't enclose your parameters in quotes - otherwise they will be treated as literals
separate the columns with commas:
sqlComm.CommandText = #"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F] SET TPA_Approver_Code=#Approver_Code, TPA_Approved_Date =#Approved_Date, TPA_Approved_Flag='Y' WHERE MDM_Invoice_Date_Dim_Key='20150206'";
Above answer is correct. But just wanted to mention, sometimes its quicker to not to use parameters (if you trust the values in variables). Like for example:
SqlCommand sqlComm = new SqlCommand();
sqlComm = myConnection.CreateCommand();
sqlComm.CommandText = #"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F]
SET TPA_Approver_Code='"+Approver_Code+"',
TPA_Approved_Date ='2015-05-06 16:24:47.870',
TPA_Approved_Flag='Y' WHERE
MDM_Invoice_Date_Dim_Key='20150206'";

inserted values on updated database are not correct - MVC

im trying to update the values of columns from the same row in my database.
Here is my code:
if (ModelState.IsValid)
{
using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd = cn.CreateCommand();
cmd.CommandText = #"Update Content Set Des='#Des', Sc='#Sc' Where ID_Img=#Id_Img";
cmd.Parameters.Add("#Des", SqlDbType.VarChar);
cmd.Parameters["#Des"].Value = model.Des;
cmd.Parameters.Add("#Sc", SqlDbType.VarChar);
cmd.Parameters["#Sc"].Value = model.Sc;
cmd.Parameters.Add("#Id_Img", SqlDbType.Int);
cmd.Parameters["#Id_Img"].Value = model.Id_Img;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
}
the code works and i get no error, but when i check in my database the values inserted are #Desc and #Sc and not properly the values which comes from form in my view.
I have set a breakpoint and check which values are in the variables all off them have the correct one..
What im doing wrong?
Someone can give me a hand pls?
Don't wrap your parameters in quotes in the actual query. Try this:
cmd.CommandText = #"Update Content Set Des=#Des, Sc=#Sc Where ID_Img=#Id_Img";
The parameters are smart enough to know if they need to be wrapped in quotes or not based on their data types.

DataAdapter.UpdateCommand not working c#?

I am using this code to update "SOME" columns in a table in my database. But everytime I try to do so an error is given.
No value given for one or more required parameters.
con.Open();
SlipDA = new OleDbDataAdapter();
string sqlUpdate = "Update tbl_Slip SET RaiseBasic=#RaiseBasic, OtherDed=#OtherDed, Arrears=#Arrears, Notes=#Notes WHERE SlipNo=#SlipNo";
SlipDA.UpdateCommand = new OleDbCommand(sqlUpdate, con);
SlipDA.UpdateCommand.Parameters.AddWithValue("#RaiseBasic", Convert.ToInt32(dRow[4].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("#OtherDed", Convert.ToInt32(dRow[5].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("#Arrears", Convert.ToInt32(dRow[7].ToString()));
SlipDA.UpdateCommand.Parameters.AddWithValue("#Notes", dRow[8].ToString());
SlipDA.UpdateCommand.Parameters.AddWithValue("#SlipNo", dRow[0].ToString());
SlipDA.UpdateCommand.ExecuteNonQuery();
con.Close();
The table contains 9 columns but I only want to update a few.
This Could be the problem :
The OLE DB .NET Provider does not support named parameters for passing
parameters to an SQL statement or a stored procedure called by an
OleDbCommand when CommandType is set to Text. In this case, the
question mark (?) placeholder must be used. For example: SELECT * FROM
Customers WHERE CustomerID = ?
source : this
so basically your query should be like this :
string sqlUpdate = "Update tbl_Slip SET RaiseBasic= ?, OtherDed= ?, Arrears= ?, Notes= ? WHERE SlipNo= ?";
try this
string sqlUpdate = "Update tbl_Slip SET RaiseBasic=#RaiseBasic, OtherDed=#OtherDed, Arrears=#Arrears, Notes=#Notes WHERE SlipNo=#SlipNo";
OleDbCommand UpdateCommand = new OleDbCommand(sqlUpdate, con);
UpdateCommand.Parameters.AddWithValue("#RaiseBasic", Convert.ToInt32(dRow[4].ToString()));
UpdateCommand.Parameters.AddWithValue("#OtherDed", Convert.ToInt32(dRow[5].ToString()));
UpdateCommand.Parameters.AddWithValue("#Arrears", Convert.ToInt32(dRow[7].ToString()));
UpdateCommand.Parameters.AddWithValue("#Notes", dRow[8].ToString());
UpdateCommand.Parameters.AddWithValue("#SlipNo", dRow[0].ToString());
con.Open();
UpdateCommand.ExecuteNonQuery();
con.Close();

OleDbDataAdapter Update To dbf [Free Table] -Syntax Error()

When I insert through the OleDbCommand with direct values no problem, it's working fine
OleDbCommand OleCmd1 = new OleDbCommand("Insert into My_Diary (sl_no,reminder) values("+a1+",'CHECK VALUE')", OleCon1);
OleCmd1->ExecuteNonQuery();
But when I like to update through parameter its showing "Syntax Error"....I can't identify my mistake...
string MyConStr = "Provider=VFPOLEDB.1; Data Source='C:\\For_Dbf'; Persist Security Info=False";
InsSavDiaryCmd = "Insert into My_Table1 (sl_no,reminder) values (#sl_no,#reminder) ";
VFPDAp=gcnew OleDbDataAdapter();
VFPDApMy_Table1InsertCommand = gcnew OleDbCommand(InsSavDiaryCmd, OleCon1);
WithInsVar = VFPDAp.InsertCommand.Parameters;
WithInsVar.Add("#sl_no", OleDbType.Integer, 10, "sl_no");
WithInsVar.Add("#reminder", OleDbType.Char, 250, "reminder");
OleCon1.ConnectionString = MyConStr;
OleCon1.Open();
OleDbTransaction Trans=OleCon1.BeginTransaction();
//VFPDAp.DeleteCommand.Transaction = Trans;
//VFPDAp.UpdateCommand.Transaction = Trans;
VFPDAp.InsertCommand.Transaction = Trans;
VFPDAp.Update(MyDataTbl);
Trans.Commit();
OleCon1.Close();
The OleDbCommand doesn't use named parameters. You need to change the insert statement so that it uses questions.
InsSavDiaryCmd = "Insert into My_Table1 (sl_no,reminder) values (?, ?) ";
You need to make sure that you have a parameter for each question mark and make sure that the parameters are inserted in order of their use in the insert statement.
** If you'd like to use name parameters... you can try using VfpClient which is a project that I'm working on to make data access a little nicer from .Net.

Insert query for ms-access with Oledb object getting exception of not a valid query

I'm using the following code and it is giving the invalid Insert command exception.
row the DataRow object to be added to the database , conn is the OleDBConnection object.
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = `"Insert Into Appointments(ID,Body,End,Location,Start,Subject,Properties)"
+ "Values(#ID,#Body,#End,#Location,#Start,#Subject,#Properties)";
cmd.Parameters.Add("#ID", OleDbType.WChar).Value = row[0].ToString();
cmd.Parameters.Add("#Body", OleDbType.WChar).Value = row[1].ToString();
cmd.Parameters.Add("#End", OleDbType.Date).Value = Convert.ToDateTime(row[2]).Date.ToLongDateString();
cmd.Parameters.Add("#Location", OleDbType.WChar).Value = row[3].ToString();
cmd.Parameters.Add("#Start", OleDbType.Date).Value = Convert.ToDateTime(row[4]).Date.ToLongDateString();
cmd.Parameters.Add("#Subject", OleDbType.WChar).Value = row[5].ToString();
cmd.Parameters.Add("#Properties", OleDbType.WChar).Value = row[6].ToString();
conn.Open();
cmd.ExecuteNonQuery(); //At this line exception is generating
conn.Close();
Please help me in this.
You've got one (possibly more) reserved word in your table's field names.
The field name End ... at the very least.
Try
cmd.CommandText = `"Insert Into Appointments(ID,Body,[End],Location,Start,Subject,Properties)"
+ "Values(#ID,#Body,#End,#Location,#Start,#Subject,#Properties)";
Does "Appointments" table support inserting ID? If ID column is the identity value, that may cause problem.
I think that the data-types that you use for your parameters are incorrect.
If your ID column is a numeric column, you shouldn't use OleDbType.WChar, but OleDbType.Integer, for instance
For alfanumeric-columns, I wouldn't use OleDbType.WChar either, but OleDbtype.VarChar.
See the OleDbType enumeration as well.

Categories