table structure
srno bigint(20)
citycode bigint(20)
centercode bigint(20)
rollno bigint(20)
firstname varchar(100)
lastname varchar(100)
fathername varchar(100)
dob datetime
gender char(1)
examdate datetime
faceimagelocation varchar(256)
keydata varchar(256)
string StrQry=Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender,dob,centerCode,examDate,faceImageLocation,keydata )
values( ?rollNo,?formNo,?citycode,?firstName,?lastName,?fatherName,?Gender,?dob,?centerCode,?examDate,?faceImageLocation,?keydata)
SqlCmd = new OdbcCommand(StrQry, SqlCon);
SqlCmd.Parameters.Add(new OdbcParameter("?" + DT.Columns[IntIncVar].ToString(), SqlDbType.Int)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
SqlCmd.Parameters.Add(new OdbcParameter("?" + DT.Columns[IntIncVar].ToString(), SqlDbType.VarChar)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
SqlCmd.Parameters.Add(new OdbcParameter("?" + DT.Columns[IntIncVar].ToString(), SqlDbType.DateTime)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
........... //as required
dt is datatable, passing parmeter field wise, i have not written all parameter here, getting right value for each column i.e., getting right value from DT.Rows[IntDTCOUNT][IntIncVar]
SqlCmd.ExecuteNonQuery();
// i am getting error as "ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.28-rc-community]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'firstName,'SHREYA CHITNAVIS'lastName,'ABHAY CHITNAVIS'fatherName,'m'Gender,_lati' at line 1"
plz let me know where i am getting wrong
note:- database is mysql
Loading

Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 11:58 PM EDT
see this code:
-------------------------
OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=test;User=test;Password=test;Option=3;"); string queryStr = "Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender," +"dob,centerCode,examDate,faceImageLocation,keydata ) values( ?,?,?,?,?,?,?,?,?,?,?,?);"; OdbcCommand cmd = new OdbcCommand(queryStr,myconnNew); myconnNew.Open(); cmd.Parameters.Add(new OdbcParameter("rollNo", "533003")); cmd.Parameters.Add(new OdbcParameter("formNo", "1")); cmd.Parameters.Add(new OdbcParameter("citycode", "9")); cmd.Parameters.Add(new OdbcParameter("firstName", "SHREYA CHITNAVIS")); cmd.Parameters.Add(new OdbcParameter("lastName", "SHREYA CHITNAVIS")); cmd.Parameters.Add(new OdbcParameter("fatherName", "ABHAY CHITNAVIS")); cmd.Parameters.Add(new OdbcParameter("Gender", "m")); cmd.Parameters.Add(new OdbcParameter("dob", "1995-05-12")); cmd.Parameters.Add(new OdbcParameter("centerCode", "9003")); cmd.Parameters.Add(new OdbcParameter("examDate", "2003-01-04")); cmd.Parameters.Add(new OdbcParameter("faceImageLocation", "c:\\VAYAPAMIMAGE\\07-07-2013\\9\\p1.jpg")); cmd.Parameters.Add(new OdbcParameter("keydata", "E5CAFC3CCF056BE4A319A1FD0FF8BE08")); cmd.ExecuteNonQuery(); myConn.Close(); ---------------------------- It's working pretty fine. Schema is also as you've mentioned, check it below: --------------------------------- CREATE TABLE `tblstudentdetails` ( `rollno` bigint(20) default NULL, `formNo` bigint(20) default NULL, `citycode` bigint(20) default NULL, `firstname` varchar(100) default NULL, `lastname` varchar(100) default NULL, `fathername` varchar(100) default NULL, `gender` char(1) default NULL, `dob` datetime default NULL, `centercode` bigint(20) default NULL, `examdate` datetime default NULL, `faceimagelocation` varchar(256) default NULL, `keydata` varchar(256) default NULL ) ---------------------------------- So what I would sugest you at this point is to check if it's getting a value for "keydata" field. Hope it helps.Ravi ShekharRank #673 · 3.6K reputationPosted Aug 2, 2013, 10:15 PM EDT
or share your values here.
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 10:28 PM EDT
@formNo 1
@citycode 9
@firstName SHREYA CHITNAVIS
@lastName SHREYA CHITNAVIS
@fatherName ABHAY CHITNAVIS
@Gender m
@dob 12/13/1995 12:00:00 AM
@centerCode 9003
@examDate 07/07/2013 12:00:00 AM
@faceImageLocation c:\VAYAPAMIMAGE\07-07-2013\9\p1.jpg
@keydata E5CAFC3CCF056BE4A319A1FD0FF8BE08
i am getting the above value for each parameter.
plz note:- if i am using @ in place of ?(see question) then null value is inserted into database and srno(as it is auto generated) goes to number of record inserted. i.e. if i insert 6 row using @ in place of ? then row will be inserted with srno value from 1-6 but all other column value are null
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 10:33 PM EDT
put a breakpoint on this line:
SqlCmd.ExecuteNonQuery();
and check the final value of query in SqlCmd.
Share more code if still have issues.
Ravi ShekharRank #673 · 3.6K reputationPosted Aug 2, 2013, 10:33 PM EDT
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 10:37 PM EDT
Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender,dob,centerCode,examDate,faceImageLocation,keydata ) values( ?rollNo,?formNo,?citycode,?firstName,?lastName,?fatherName,?Gender,?dob,?centerCode,?examDate,?faceImageLocation,?keydata)
at SqlCmd.ExecuteNonQuery();
on debugging i am getting each parameter value as i have mentioned in my last post.
@Ravi Shekhar
how i can put ' ' on this query?
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 10:39 PM EDT
See the error:
"'firstName,'SHREYA CHITNAVIS'lastName,'ABHAY CHITNAVIS'fatherName,'m'Gender,_lati' at line 1"
I don't see the statement in proper format. I mean they are not properly quoted, missing the commas somewhere. Check the logic carefully.
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 10:48 PM EDT
on debuging the value i get have written on earlier post. i am getting insert statement as posted, and value on debugging as posted. i dont see any missing or syntax error in that, if exists kindly point out
when i use ? in place of @ i get this error but on using @, executenonquery doesnot throw any error. instead it inserts row with null value for all column.
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 10:55 PM EDT
I've few questions for you just to troubleshoot the issue:
1. Are you using a MySqlConnection or SqlConnection?
2. What is the point of adding OdbcParameter instead of MySqlParameter if it's a MySql database?
Ravi ShekharRank #673 · 3.6K reputationPosted Aug 2, 2013, 11:00 PM EDT
Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender,dob,
centerCode,examDate,faceImageLocation,'keydata' )
values( ?rollNo,?formNo,?citycode,?firstName,?lastName,?fatherName,?Gender,?dob,?centerCode,?examDate,?faceImageLocation,?keydata)
I think keydata is reserved keyword.
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:00 PM EDT
m using OdbcConnection
Ravi ShekharRank #673 · 3.6K reputationPosted Aug 2, 2013, 11:03 PM EDT
Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,'Gender',dob,
centerCode,examDate,faceImageLocation,'keydata' )
values( ?rollNo,?formNo,?citycode,?firstName,?lastName,?fatherName,?Gender,?dob,?centerCode,?examDate,?faceImageLocation,?keydata)
here gender is also a keyword. so change. Gender as 'Gender'
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 11:05 PM EDT
http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html
Ravi ShekharRank #673 · 3.6K reputationPosted Aug 2, 2013, 11:11 PM EDT
you tried or not????
this error occurs because of reserved keywords or inserting wrong datatype value
check below link
http://stackoverflow.com/questions/3843362/problem-with-odbc-net-connection-to-mysql-db
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 11:15 PM EDT
modify your code like below:
------------------------
string StrQry=Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender,dob,centerCode,examDate,faceImageLocation,keydata )
values(?,?,?,?,?,?,?,?,?,?,?,?)
SqlCmd = new OdbcCommand(StrQry, SqlCon);
//replace these lines with I've menioned below it: (these codes are also fine but just to cut the lines)
SqlCmd.Parameters.Add(new OdbcParameter("?" + DT.Columns[IntIncVar].ToString(), SqlDbType.Int)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
SqlCmd.Parameters.Add(new OdbcParameter("?" + DT.Columns[IntIncVar].ToString(), SqlDbType.VarChar)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
use this line:
SqlCmd.Parameters.AddWithValue("rollno",DT.Rows[IntDTCOUNT][IntIncVar]);
SqlCmd.Parameters.AddWithValue("formno",DT.Rows[IntDTCOUNT][IntIncVar]);
// so on.
Feedback awaited.
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:19 PM EDT
i tried but of no use
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 11:21 PM EDT
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:24 PM EDT
tried and done except for one issue working fine but last column value i.e, keydata value is not getting inserted. all other value are getting inserted perfectly. just this issue remains. let me check the syntax.
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 11:26 PM EDT
can you try like this
Regards,
Iftikar
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:29 PM EDT
where datatoencrypt=E5CAFC3CCF056BE4A319A1FD0FF8BE08
insert command has 12 ? against 12 field
Insert into tblstudentDetails ( rollNo,formNo,citycode,firstName,lastName,fatherName,Gender,dob,centerCode,examDate,faceImageLocation,keydata ) values( ?,?,?,?,?,?,?,?,?,?,?,?)
still keydata is not getting inserted
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 11:36 PM EDT
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:41 PM EDT
all other column value are inserted perfectly just this column has issue
for key data m using below line
SqlCmd.Parameters.Add(new OdbcParameter("@keyData", SqlDbType.VarChar)).Value = dataToEncrypt.ToString();
and for other field m using below line based on their datatype
SqlCmd.Parameters.Add(new OdbcParameter("@" + DT.Columns[IntIncVar].ToString(), SqlDbType.VarChar)).Value = DT.Rows[IntDTCOUNT][IntIncVar];
m i missing something above
datatoencrypt value is E5CAFC3CCF056BE4A319A1FD0FF8BE08
datatoencrypt variable is of string type
in database
keydata datatype is varchar(256)
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 11:43 PM EDT
............................
.............................
and also make your table column size enough to hold keydata value
Regards,
Iftikar
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 11:46 PM EDT
for key data m using below line
SqlCmd.Parameters.AddWithValue("keyData", dataToEncrypt.ToString()).;
Regards,
Iftikar
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:50 PM EDT
OdbcType.VarChar, 256 is not working
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:52 PM EDT
not working
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 11:53 PM EDT
Regards,
Iftikar
nishant ranjanRank #4,961 · 317 reputationPosted Aug 2, 2013, 11:58 PM EDT
SqlCmd.Parameters.Add("@keydata", OdbcType.VarChar, 256);
SqlCmd.Parameters["@keydata"].Value = "Test";
still null value is getting inserted
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 11:59 PM EDT
Can you check again?
Regards,
Iftikar
nishant ranjanRank #4,961 · 317 reputationPosted Aug 3, 2013, 12:18 AM EDT
its varchar
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 3, 2013, 12:25 AM EDT
any update on that?
nishant ranjanRank #4,961 · 317 reputationPosted Aug 3, 2013, 12:25 AM EDT
Done!!!
but y it was not working with sqldbtype???
anyway... thanks to all for the suggestions.
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 3, 2013, 12:39 AM EDT
I'm glad I could help, Happy Coding :)
Cheers!!
nishant ranjanRank #4,961 · 317 reputationPosted Aug 3, 2013, 1:27 AM EDT
Thanks
Cheers!!!