Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//What's wrong in code !!!
protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conx;
            conx = new MySqlConnection("server=localhost; database=pt; user id=root;");                      
            
            MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);

            SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar,50);
            SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar,45);
            
            SimpanAdmin.Parameters["@A"].Value = TextBox1.Text;
            SimpanAdmin.Parameters["@AB"].Value = TextBox2.Text;
            chy.Text = "Proses Menyimpan Berhasil...!!!";
            
            try
            {
                SimpanAdmin.Connection.Open();
                SimpanAdmin.ExecuteNonQuery();
                SimpanAdmin.Connection.Close();                                
            }
            catch
            {
                chy.Text = "Proses Menyimpan Gagal...!!!";
            }
        }


// Can you help me for wrong code...i am finish for build but cannot save in MySql database :confused::confused:
Posted
Updated 6-Feb-11 14:58pm
v2
Comments
Nish Nishant 6-Feb-11 21:39pm    
What exception gets thrown? Catch it instead of swallowing it using the parameter-less catch.
Henry Minute 6-Feb-11 23:05pm    
After a quick glance, your code looks OK. So, as Nishant says, we need to see what the error is before we can help.

You try the following code ,It may be helpful

 protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conx;
            conx = new MySqlConnection("server=localhost; database=pt; user id=root;");

            try
            {
                
                MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);
SimpanAdmin.Connection.Open();
                SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar, 50).Value = TextBox1.Text;
                SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar, 45).Value = TextBox2.Text;
                chy.Text = "Proses Menyimpan Berhasil...!!!";
                SimpanAdmin.ExecuteNonQuery();
            }
            catch
            {
                chy.Text = "Proses Menyimpan Gagal...!!!";
            }
            finally { SimpanAdmin.Connection.Close();
}
        }


Best Regards,
Theingi Win.
 
Share this answer
 
v4
Comments
Henry Minute 6-Feb-11 23:03pm    
Does moving the Open() method up to before the Parameters are added make any difference in MySql. It would certainly not alter anything in SqlServer.
Theingi Win 7-Feb-11 0:36am    
Thanks! Henry Minute

Now i already Modified.

Hello,

I have the same problem, my solution was to change @ to ? like:
protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conx;
            conx = new MySqlConnection("server=localhost; database=pt; user id=root;");                      
            
            MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (?A,?AB)", conx);

            SimpanAdmin.Parameters.Add("?A", MySqlDbType.VarChar,50).Value = TextBox1.Text;
            SimpanAdmin.Parameters.Add("?AB", MySqlDbType.VarChar,45).Value = TextBox2.Text;
            
            chy.Text = "Proses Menyimpan Berhasil...!!!";
            
            try
            {
                SimpanAdmin.Connection.Open();
                SimpanAdmin.ExecuteNonQuery();
                SimpanAdmin.Connection.Close();                                
            }
            catch
            {
                chy.Text = "Proses Menyimpan Gagal...!!!";
            }
        }


HTH
 
Share this answer
 
Old question but from here:
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html[^]

Note. Prior versions of the provider used the '@' symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the '?' symbol to locate parameters in SQL. To support older code, you can set 'old syntax=yes' on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL.
 
Share this answer
 
Your Are long my question...

//==This My Answer==
C#
MySqlConnection conx;
            conx = new MySqlConnection("server=localhost; database=pt; user id=root;");

            MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values ('" + TextBox1.Text + "', '" + TextBox2.Text + "')", conx);

            chy.Text = "Proses Menyimpan Berhasil...!!!";
            
            try
            {
                SimpanAdmin.Connection.Open();
                SimpanAdmin.ExecuteNonQuery();
                SimpanAdmin.Connection.Close();                                
            }
            catch
            {
                chy.Text = "Proses Menyimpan Gagal...!!!";
            }

// :-\ :-\ :-\ :-\ :-\ :-O :-O :-O :-O :-O :) :) :) :) :thumbsup::thumbsup::thumbsup::thumbsup:
 
Share this answer
 
v2
Comments
Theingi Win 6-Feb-11 21:45pm    
Good Job!
cohay_indonesia 6-Feb-11 21:50pm    
Thx for participation and see my post ^_^.
Henry Minute 6-Feb-11 23:01pm    
This is not a good solution. It leaves the code susceptible to SQL Injection attacks. Parameterized queries, as used by the OP, are the safer way to do this.
cohay_indonesia 6-Feb-11 23:32pm    
You have good solution..? Please give me code for good solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900