INSERT into MYSQL results to NULL values.. I NEED HELP!!!

Last post 07-13-2007 2:46 PM by Mikesdotnetting. 6 replies.

Sort Posts:

  • INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    05-14-2007, 8:31 AM
    • Loading...
    • jmkerr12
    • Joined on 05-14-2007, 12:19 PM
    • Posts 4

    why does this code generates null values on the mysql table... i can't figure out why it does not insert the right inputs... please help me!!

     

            string strConnectionString = ConfigurationManager.ConnectionStrings["OdbcConnectionString"].ConnectionString;
            OdbcConnection myConnection = new OdbcConnection(strConnectionString);
            myConnection.Open();
            string strQuery = "INSERT INTO trydatabase(Names,Addresses) VALUES (@Namex, @Addressex)";

            OdbcCommand myCommand = new OdbcCommand(strQuery, myConnection);

           
            myCommand.Parameters.AddWithValue("@Namex", txtNames.Text);
            myCommand.Parameters.AddWithValue("@Addressex", txtAddresses.Text);

            myCommand.ExecuteNonQuery();
            myConnection.Close();

     

     i have already tried replacing @ with a ? but it does not help... it generate this error::

     ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.41-community-nt]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 'Namex, 'ssss'Addressex)' at line 1

     

    please help me with this,, i really need everybody's help..
     

  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    05-14-2007, 9:14 AM
    Answer

    I assume that you have verified that txtNames.Text and txtAddresses.Text have values to pass to the parameters?

    Otherwise try this:

    string strConnectionString = ConfigurationManager.ConnectionStrings["OdbcConnectionString"].ConnectionString;
            OdbcConnection myConnection = new OdbcConnection(strConnectionString);
            myConnection.Open();
            string strQuery = "INSERT INTO trydatabase(Names,Addresses) VALUES (?, ?)";
            OdbcCommand myCommand = new OdbcCommand(strQuery, myConnection);
            myCommand.Parameters.AddWithValue("", txtNames.Text);
            myCommand.Parameters.AddWithValue("", txtAddresses.Text);
            myCommand.ExecuteNonQuery();
            myConnection.Close();

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    05-14-2007, 9:27 PM
    • Loading...
    • jmkerr12
    • Joined on 05-14-2007, 12:19 PM
    • Posts 4
    WoW!!! it worked.. thank you Mikesdotnetting.. Thanks a lot!!!
  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    05-26-2007, 12:02 PM
    • Loading...
    • MisterT
    • Joined on 05-26-2007, 3:54 PM
    • France
    • Posts 11

    Ok that works  but if you have a lot of parameters it's difficult to code using that way.

    I don't find why but MySql doesn't like character '@' in statement. So to name each parameter, you can use an other one : '?' for instance.

    Then you code could work

    1            string strConnectionString = ConfigurationManager.ConnectionStrings["OdbcConnectionString"].ConnectionString;
    2            OdbcConnection myConnection = new OdbcConnection(strConnectionString);
    3            myConnection.Open();
    4            string strQuery = "INSERT INTO trydatabase(Names,Addresses) VALUES (?Namex, ?Addressex)";
    5   
    6            OdbcCommand myCommand = new OdbcCommand(strQuery, myConnection);
    7   
    8          
    9            myCommand.Parameters.AddWithValue("?Namex", txtNames.Text);
    10           myCommand.Parameters.AddWithValue("?Addressex", txtAddresses.Text);
    11  
    12           myCommand.ExecuteNonQuery();
    13           myConnection.Close();  

     
    MisterT
    --
    e-commerce logistics solution
    Crosslog.com - solution logistique e-commerce
  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    07-01-2007, 1:09 AM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    Here is my code below...I need similar help as well.  I've seen this thread before and not sure how to apply it.  I don't get any errors and I know it gets through the code because when I click my Insert button (of type "link"), I get redirected to my defaultmainpage.aspx.  I don't get any data into the actual table though.  My declarations are fine and my connection string is fine as well because I use it elsewhere and works just fine.  I'm thinking someone is off.  Could use some help on this one! 

    myConnection = New MySqlConnection(MySQLConnectionString)
    'qrySelect = "SELECT Title, Category, Description, Price, Phone, Email, State, UserPassword, AdNum FROM Ads"
    qryInsert = "INSERT INTO Ads (Title, Category, Description, Price, Phone, Email, State, UserPassword) VALUES (?Title, ?Category, ?Description, ?Price, ?Phone, ?Email, ?State, ?UserPassword)"
    qrySelect = "SELECT Title, Category, Description, Price, Phone, Email, State, UserPassword, AdNum FROM Ads"
    'Dim cmdDel As MySqlCommand = New MySqlCommand(strDel, New MySqlConnection(DBC))
    myDataAdapter = New MySqlDataAdapter(qrySelect, myConnection)
    myDataSet =
    New Dataset()
    myDataAdapter.Fill(myDataSet,
    "Ads")
    Dim iCmd As MySql.Data.MySqlClient.MySqlCommand
    'Dim iCmd As MySqlCommand
    iCmd = New MySqlCommand(qryInsert, myConnection)
    'iCmd.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("?Title", MySql.Data.MySqlClient.MySqlDbType.VarChar, 50, "Title"))
    iCmd.Parameters.Add(New MySqlParameter("?Title", MySqlDbType.VarChar, 50, "Title"))
    'iCmd = New MySqlCommand(qryInsert, myConnection)
    'iCmd.Parameters.Add(New MySql.Data.MySqlClient.MySqlParameter("?Category", MySql.Data.MySqlClient.MySqlDbType.VarChar, 50, "Category"))
    iCmd.Parameters.Add(New MySqlParameter("?Category", MySqlDbType.VarChar, 50, "Category"))
    iCmd.Parameters.Add(
    New MySqlParameter("?Description", MySqlDbType.VarChar, 50, "Description"))
    iCmd.Parameters.Add(
    New MySqlParameter("?Price", MySqlDbType.VarChar, 50, "Price"))
    iCmd.Parameters.Add(
    New MySqlParameter("?Phone", MySqlDbType.VarChar, 50, "Phone"))
    iCmd.Parameters.Add(
    New MySqlParameter("?Email", MySqlDbType.VarChar, 50, "Email"))
    iCmd.Parameters.Add(
    New MySqlParameter("?State", MySqlDbType.VarChar, 50, "State"))
    iCmd.Parameters.Add(
    New MySqlParameter("?UserPassword", MySqlDbType.VarChar, 50, "UserPassword"))
    myDataAdapter.InsertCommand = iCmd
    MySQLDataGrid.DataSource = myDataSet.Tables(
    "Ads").DefaultView
    MySQLDataGrid.DataBind()

    Response.Redirect(
    "maindefault.aspx")

    I am thinking that it is something simple.  This code gets called after an Insert button is clicked on my DataGrid...

  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    07-13-2007, 12:41 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    I still need help - anyone out there?

  • Re: INSERT into MYSQL results to NULL values.. I NEED HELP!!!

    07-13-2007, 2:46 PM

    For future reference, you shouldn't add your question to an existing thread.  I noticed that you haven't made many posts, so you are probably unaware that this is generally discouraged for the following reasons:

    <pre-canned reasons coming up...> 

    1. It's called "thead-jacking" and is considered a breach of netiquette.
    2. Your issue is different to the one discussed in this thread.  If it was the same, this thread will eventually, or would already have provided you with an answer.
    3. Introducing your question to someone else's thread may appear to be an attempt to avoid moderation.
    4. Introducing your question to someone else's thread may be considered by some as "butting in".
    5. Introducing your question to someone else's thread is like saying "never mind them, what about ME".
    6. People in the future who are searching for a solution similar to you will be helped with a more appropriately titled thread.
    7. Only the original poster has the ability to mark a thread as resolved.  If the original question was still unresolved, but someone resolved yours, the thread would still remain marked as unresolved.  That will be confusing.
    8. As the thread progresses, it will become increasingly difficult to work out who is responding to which question.
    9. The logical result of thread jacking is that each forum would consist of one long thread containing thousands of posts all related to the forum title, but completely useless from a reader's point of view
    10. In nearly every case, thread-jack posts added to old threads are ignored by everyone, especially if the thread has already been marked as resolved

    </precanned reasons finished>

    A far as your Insert command is concerned, I don't know why you are involving dataadapters or datasets.  Just call cmd.executenonquery to perform an insert.  Also, if you are binding a dataset to a bunch of data, your call to Response.Redirect will kill it off as soon as your are redirected.

    If you can't make any more progress with the above, start a new thread, and explain exactly what you  are trying to achieve (because it's not that clear), and when posting code, remove commented lines.  Only post the minimum amount of code required for others to help with your issue. 

    PS.  The pre-canned reasons are meant to be general and informative, not specific to or critical of you. Big Smile

    Regards Mike
    [MVP - ASP/ASP.NET]
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter