CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2011
    Posts
    3

    Insert into MySql Table

    Im sorry if you have seen this question a thousand times, But ive googled this and still couldnt find a solution. I googled and changed my code a bit but it still doesnt fix my code.

    So,i want insert data into my Mysql table from an asp.net page But it kept giving me errors, the lastest of which is "Only MySqlParamaters objects may be stored".

    Here is my Code:

    MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySql"].ConnectionString);

    string com = "INSERT INTO Users(Name, Surname, Pass, Email, Type)
    VALUES(@Name,@Surname,@Password,@Emailaddr, @type)";

    MySqlCommand Com = new MySqlCommand(com, con);

    Com.CommandType = CommandType.Text;
    Com.Parameters.Add(new MySqlParameter("?Name", MySqlDbType.Text).Value = txtfname.Text);
    Com.Parameters.Add(new MySqlParameter("?Surname", MySqlDbType.Text).Value = txtlname.Text);
    Com.Parameters.Add(new MySqlParameter("?Password", MySqlDbType.Text).Value = txtpass11.Text);
    Com.Parameters.Add(new MySqlParameter("?Emailaddr", MySqlDbType.Text).Value = txtemail.Text);
    Com.Parameters.Add(new MySqlParameter("?type", MySqlDbType.Text).Value = "Patient");

    con.Open();
    Com.ExecuteNonQuery();

    con.Close();

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Insert into MySql Table

    Quote Originally Posted by Raidzen10 View Post
    [...]
    string com = "INSERT INTO Users(Name, Surname, Pass, Email, Type)
    VALUES(@Name,@Surname,@Password,@Emailaddr, @type)";

    MySqlCommand Com = new MySqlCommand(com, con);

    Com.CommandType = CommandType.Text;
    Com.Parameters.Add(new MySqlParameter("?Name", MySqlDbType.Text).Value = txtfname.Text);
    [..]
    First: I do believe you have a matter due to the use of the "@" parameter that mySql does not like.
    In any case, you should be consistent with yourself : you used "?" in the parameters, and you should use it also in the query, like in the following example:
    Code:
    string theQuery = "INSERT INTO Users(Name, Surname, Pass, Email, Type)
    VALUES(?Name,?Surname,?Password,?Emailaddr, ?type)";
    Second:
    you have some special names there:
    Name
    and expecially
    Type
    you should surround them in brackets : "[" and "]"
    Code:
    string theQuery = "INSERT INTO Users([Name], Surname, Pass, Email, [Type])
    VALUES(?Name,?Surname,?Password,?Emailaddr, ?type)";
    Third (less important, this is to make your life better, you can completely disregard the following)
    c# is case sensitive, thus you might have a variable named "com" foro the commandString and a variable named "Com" for the Command Object. But let me suggest you to name one of the two in a different way
    Last edited by Cimperiali; March 27th, 2012 at 09:36 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured