| | |
MySql table data always null values on INSERT
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 920
Reputation:
Solved Threads: 147
A few days ago I decided to install MySql v1.2.17 and I downloaded a client class library: MySql.Data.dll v5.0.9.0 (by MySQL AB). I included the client classes inside my wrapper class and had it creating, dropping, inserting, and deleting tables..., or so I thought. I guess I never actually looked at the records it was creating from my test class.
Here is the problem: All my row data is always NULL values and I cannot figure out why this is! I broke out the wrapper code and placed base class calls into a simple set of statements as represented here:
I don't get any errors, just NULL values in my columns. I have full schema privileges for the database and am able drop, create, and select just fine--though all the values are NULL because that is how they are being inserted.
Any ideas?
Here is the problem: All my row data is always NULL values and I cannot figure out why this is! I broke out the wrapper code and placed base class calls into a simple set of statements as represented here:
C# Syntax (Toggle Plain Text)
public static void MySqlTestInsert() { string connStr = TestDbWrapper.BuildConnectionString(DbWrapperType.MySql); string insertCmd = "INSERT INTO TestDbWrapper " + "(FieldInt32, FieldVarchar_50, FieldBoolean, FieldDateTime) Values " + "(@FieldInt32, @FieldVarchar_50, @FieldBoolean, @FieldDateTime)"; using (MySqlConnection conn = (MySqlConnection)DbWrapper.GetDbConnection(DbWrapperType.MySql, connStr)) { try { conn.Open(); using (MySqlCommand cmd = new MySqlCommand(insertCmd, conn)) { cmd.Parameters.Add(new MySqlParameter("@FieldInt32", 10)); cmd.Parameters.Add(new MySqlParameter("@FieldVarchar_50", "some text...")); cmd.Parameters.Add(new MySqlParameter("@FieldBoolean", true)); cmd.Parameters.Add(new MySqlParameter("@FieldDateTime", DateTime.Now)); cmd.ExecuteNonQuery(); } } catch (DbException ex) { Console.WriteLine("Exception: {0}\r\n Stack Trace: {1}", ex.Message, ex.StackTrace); } finally { conn.Close(); } } }
I don't get any errors, just NULL values in my columns. I have full schema privileges for the database and am able drop, create, and select just fine--though all the values are NULL because that is how they are being inserted.
Any ideas?
•
•
Join Date: Sep 2004
Posts: 13
Reputation:
Solved Threads: 1
0
#2 31 Days Ago
Have you tried using one of the other overloaded versions of Adding parameters where you actually specify the data type of what you are inserting? Might be worth a try.
Ex:
Ex:
C# Syntax (Toggle Plain Text)
cmd.Parameters.Add(new MySqlParameter("@FieldInt32", SqlDbType.Int, 10));
0
#3 31 Days Ago
MySQL is bad for your eyes.I don't know anything about it but typically you add parameters like this:
C# Syntax (Toggle Plain Text)
conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = MemberId; cmd.Parameters.Add(new SqlParameter("@MemberSname", SqlDbType.VarChar)).Value = MemberSname; cmd.Parameters.Add(new SqlParameter("@MemberFName", SqlDbType.VarChar)).Value = MemberFName; cmd.Parameters.Add(new SqlParameter("@Picture", SqlDbType.VarChar)).Value = Picture; cmd.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar)).Value = FName; cmd.ExecuteNonQuery(); }
Notice how the ctor is (Name, DataType) and the value is set from a result of the Add() method returning an instance of the parameter? I googled around a little and saw other mysql'ers doing it the same way but I don't have those libraries to test with.
Last edited by sknake; 31 Days Ago at 2:08 pm.
0
#5 31 Days Ago
Oh. ODBC driver's don't support named parameters (i'm 99% sure) and only limited OleDb drivers do. Unless it is a microsoft driver usually @Param is not supported. I forgot about that.
![]() |
Similar Threads
- How to insert date in string format into MYSQL table as DATE data type (MySQL)
- Creating a MySQL table and Entity from Database. (JSP)
- way to inserting data into a MYSQL table data in an ordered way (MySQL)
- how to update data in mysql table using data from an excel table (MySQL)
- updating multiple columns in single MySQL table (PHP)
- Currency on MySQL table (MySQL)
- adding data from an array to mysql table (PHP)
- Populating a MySQL table with data from a csv file (PHP)
Other Threads in the C# Forum
- Previous Thread: List of Objects !!!
- Next Thread: Returning an array of objects
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combo combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing encryption enum equation event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mathematics mouseclick mysql nargalax operator path photoshop picturebox pixelinversion post programming radians regex remote remoting restore richtextbox save saving serialization server sleep socket sql stack statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






