1,105,363 Community Members

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

Member Avatar
junharefa
Newbie Poster
5 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to insert data from the textBox to MySQL 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 MySQL database.

Thank You in Advance,

Attachments save1.png 36.19KB database.png 30.2KB
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

DateTime column is causing the problems I would say. YOu only do "DateTime.Now" - no parsing to string.

btw, what error you get in catch Exception?

Member Avatar
junharefa
Newbie Poster
5 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The Error Says:
Can't connect to database.
Mysql.Data.MysqlClient.MySqlException: No database selected at MySql.Data.MySqlClient.MySqlStream.ReadPacket()..
.....
.....

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

It seems your connection string to the database is not correct. And as far as i can see you use mySql database (on phpmyadmin).
Try this form is connection string:

"server=localhost;user id=root;password=secret;database=yourDataBaseName;"
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: