Click here to Skip to main content
15,913,240 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i insert in Mysql table using parameterized query in c#.net windows application project?
i have used these given below codes but in table NULL value rows inserted every time.please help!!!

C#
//C# Code for windows Application
string sql = "insert into nationality(NAME)values(@NAME)";
OdbcCommand cmd = new OdbcCommand(sql,con);
cmd.Parameters.AddWithValue("@NAME", "ABC");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//C# Code for windows Application


SQL
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `nationality`
-- ----------------------------
DROP TABLE IF EXISTS `nationality`;
CREATE TABLE `nationality` (
  `NAME` varchar(255) DEFAULT NULL,
  `CODE` int(11) DEFAULT NULL,
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of nationality
-- ----------------------------
Posted
Updated 11-May-15 2:55am
v5
Comments
_Asif_ 11-May-15 7:24am    
Why are you passing code parameter? You already have set the column in the table as AUTO INCREMENT!
BIBASWAN 11-May-15 8:34am    
see i have update my question..auto increment is working but in the name field its showing NULL

1 solution

I'm not sure if this will fix the null-row-issue but it's definitely one issue of your code:

In your CREATE TABLE-statement you've declared CODE as an integer column but you assign a String-value ("123") to the according parameter. It would probably work if you removed the quotes (123) but not work like you intended since you defined CODE also as AUTO_INCREMENT[^]. You probably don't want to specify CODE in your INSERT-statement at all so it will be assigned a value by MySQL automatically. (Or, if you do want to specify the values yourself, remove the AUTO_INCREMENT-keyword from the column-definition.)

edit: this answer refers to an earlier version of the question
 
Share this answer
 
v2
Comments
BIBASWAN 11-May-15 8:34am    
see i have update my question..auto increment is working but in the name field its showing NULL
Sascha Lefèvre 11-May-15 8:48am    
If you haven't already, please change your sql-statement like so:
"insert into nationality (NAME) values (@NAME)"

Also, please enclose your code in a try-catch-block:
try {
// your posted code here
} catch (Exception ex) {
System.Diagnostics.Debugger.Break();
}

Run your program in debug mode and see if it stops in the catch-block. If so, please tell me the message of the exception.
BIBASWAN 11-May-15 9:00am    
sir no error given...i am using odbc driver 5.1 is this driver is ok???
Sascha Lefèvre 11-May-15 9:10am    
I guess so. You would probably get an exception if the driver wasn't ok.

Please try this (the '@' removed):
cmd.Parameters.AddWithValue("NAME", "ABC");

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