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: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

MySql table data always null values on INSERT

 
0
  #1
31 Days Ago
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:

  1. public static void MySqlTestInsert()
  2. {
  3. string connStr = TestDbWrapper.BuildConnectionString(DbWrapperType.MySql);
  4.  
  5. string insertCmd = "INSERT INTO TestDbWrapper " +
  6. "(FieldInt32, FieldVarchar_50, FieldBoolean, FieldDateTime) Values " +
  7. "(@FieldInt32, @FieldVarchar_50, @FieldBoolean, @FieldDateTime)";
  8. using (MySqlConnection conn = (MySqlConnection)DbWrapper.GetDbConnection(DbWrapperType.MySql, connStr))
  9. {
  10. try
  11. {
  12. conn.Open();
  13.  
  14. using (MySqlCommand cmd = new MySqlCommand(insertCmd, conn))
  15. {
  16. cmd.Parameters.Add(new MySqlParameter("@FieldInt32", 10));
  17. cmd.Parameters.Add(new MySqlParameter("@FieldVarchar_50", "some text..."));
  18. cmd.Parameters.Add(new MySqlParameter("@FieldBoolean", true));
  19. cmd.Parameters.Add(new MySqlParameter("@FieldDateTime", DateTime.Now));
  20.  
  21. cmd.ExecuteNonQuery();
  22. }
  23. }
  24. catch (DbException ex)
  25. {
  26. Console.WriteLine("Exception: {0}\r\n Stack Trace: {1}", ex.Message, ex.StackTrace);
  27. }
  28. finally
  29. {
  30. conn.Close();
  31. }
  32. }
  33. }

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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 13
Reputation: Elbudster is an unknown quantity at this point 
Solved Threads: 1
Elbudster Elbudster is offline Offline
Newbie Poster
 
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:

  1. cmd.Parameters.Add(new MySqlParameter("@FieldInt32", SqlDbType.Int, 10));
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,264
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 582
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
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:
  1. conn.Open();
  2. using (SqlCommand cmd = new SqlCommand(query, conn))
  3. {
  4. cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = MemberId;
  5. cmd.Parameters.Add(new SqlParameter("@MemberSname", SqlDbType.VarChar)).Value = MemberSname;
  6. cmd.Parameters.Add(new SqlParameter("@MemberFName", SqlDbType.VarChar)).Value = MemberFName;
  7. cmd.Parameters.Add(new SqlParameter("@Picture", SqlDbType.VarChar)).Value = Picture;
  8. cmd.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar)).Value = FName;
  9. cmd.ExecuteNonQuery();
  10. }

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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 920
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #4
31 Days Ago
Thanks for the suggestions, and I did try them. Neither worked, so I looked a little more on the inet and decided to try using the "?" delimiter in place of the "@" symbol--that fixed the problem.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,264
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 582
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC