Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2012, 04:41 AM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Inserting data into MySQL with ASP.NET + C#

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?

The problem occurs when I want to run multiple inserts in mysql table.

Thank you in advance, I hope your help.
Quote:
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();

    }

Last edited by cms9651; November 9th, 2012 at 05:06 AM..
 
Old November 9th, 2012, 11:50 AM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

I have solved using the parameters in MySQL:
http://www.devart.com/dotconnect/mysql/docs/Parameters.html





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting an input box in asp.net mungur Visual Web Developer 2008 1 November 11th, 2008 08:58 AM
Inserting data using VB.net and sql express saif44 Visual Basic 2005 Basics 6 June 18th, 2007 08:23 PM
inserting XML/ASCII data into SQL using VB.net outcast1881 Other Programming Languages 0 July 20th, 2006 07:39 AM
Problem inserting blob data/uploading file - MySQL kyle_shea Beginning PHP 2 February 15th, 2005 10:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.