SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Inserting data into MySQL with ASP.NET + C#

    Hello there, I need your help.

    I have error and problem when tried insert into mysql table data of gridview.

    If tried th same query in mysql workbench I don't have problem, instead the aspx page print this error, why?
    Thank you in advance, I hope your help.
    ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.51-community]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 'INSERT INTO tbl_g (q) VALUES (2)' at line 1
    The code behind
    Code:
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder strSql = new System.Text.StringBuilder(string.Empty);
            OdbcCommand cmd = new OdbcCommand();
    
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {
                        strID = GridView1.Rows[i].Cells[1].Text;
                        strName = ((TextBox)
    
                            GridView1.Rows[i].FindControl("p")).Text;
                        string strQuery = "INSERT INTO tbl_g (q) VALUES (" + strName + ");";
    
                        Response.Write(strQuery);
                        strSql.Append(strQuery);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = myConnectionString;
                myConnectionString.Open();
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string errorMsg = "Error";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                myConnectionString.Close();
            }
            UncheckAll();
    
        }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The problem occurs when I want to run multiple inserts in mysql table.
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I have solved using the parameters in MySQL:
    http://www.devart.com/dotconnect/mysql/docs/Parameters.html
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

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