[Help] How to insert data from the textBox to MySQL Database in C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [Help] How to insert data from the textBox to MySQL Database in C#

  1. #1
    Join Date
    Aug 2011
    Posts
    5

    [Help] How to insert data from the textBox to MySQL Database in C#

    I want to insert data from the textBox to MySQL Database, but the data can't be inserted into the database. Here is the code snipped that I have created.
    private void button6_Click(object sender, EventArgs e)
    {
    string connectionSQL = "server=localhost;database=db_junisman_kulit;uid=root;password=;";
    MySqlConnection conn = new MySqlConnection();
    double no_simpan = 0;
    try
    {
    conn.Open();
    MySqlCommand cmd = new MySqlCommand("INSERT INTO tb_backorder (no_simpan,tanggal_simpan,jumlah_rata_rata,lead_time,ongkos_pesan,harga_barang,ongkos_simpan,ongkos_kekurangan_barang,jumlah_pemesanan,reorder_point,safety_stock,persentase_pelayanan,ongkos_total) VALUES ('" + no_simpan + "', '" + DateTime.Now.ToString("yyyyMMdd") + "', '" + textBox1.Text + "', '" + textBox3.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "', '" + textBox7.Text + "', '" + textBox8.Text + "', '" + textBox9.Text + "', '" + textBox10.Text + "', '" + textBox11.Text + "', '" + textBox12.Text + "', '" + textBox13.Text + "')", conn);
    conn.Close();
    }

    catch (MySqlException ex)
    {

    MessageBox.Show("Can't connect to database\n" + ex.ToString());
    }
    }

    for no_simpan, this data is not from the textBox but a sequence number that is automatically ordered if the data is stored to the database.

    and for tanggal_simpan is the date when the data is stored to the database.
    How do I write the correct code to insert the data from the textBox into the MySQL database? I use a MySQL database.

    I have attached form display and tb_backorder picture below
    Thank You in Advance,
    Attached Images Attached Images   

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: [Help] How to insert data from the textBox to MySQL Database in C#

    You say the data can't be inserted, what is the message you receive if any when you attempt this?

    Edit: After looking a bit longer it appears that you are not executing anything. You are creating a cmd but you are not executing it. So I am thinking that there is no message and nothing happens. Look up how to execute sql cmd
    Last edited by DataMiser; August 20th, 2011 at 08:53 AM.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: [Help] How to insert data from the textBox to MySQL Database in C#

    ..In addition to what DataMiser told you... I also notices while you're concating your sql statement, you are surrounding your no_simpan value with ' ' marks, if the field type is numeric (or decimal, int, etc... not a string), you can drop the surrounding ticks from the value.

    Edit: And... If no_simpan is an auto-incrementing row identifier, you wouldn't include that field in your INSERT anyhow... the database will take care of that once the insert is executed.
    Last edited by fcronin; August 20th, 2011 at 12:16 PM. Reason: ..another thought...

  4. #4
    Join Date
    Aug 2011
    Posts
    5

    Re: [Help] How to insert data from the textBox to MySQL Database in C#

    Quote Originally Posted by DataMiser View Post
    You say the data can't be inserted, what is the message you receive if any when you attempt this?

    Edit: After looking a bit longer it appears that you are not executing anything. You are creating a cmd but you are not executing it. So I am thinking that there is no message and nothing happens. Look up how to execute sql cmd
    @DataMizer >> Thank you so much... It work...
    @fcronin >> after I remove no_simpan from my INSERT field, It work... Thank You so much...

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center