MySQL / C # input error

Not sure what's wrong with this

//Setup MySQL private MySqlConnection datconnection = new MySqlConnection(); //private MySqlDataAdapter datdata = new MySqlDataAdapter(); DataGrid datgridInfo = new DataGrid(); int connect; private Form_NewEnquiry frm_parent; public Form_AddVenue(Form_NewEnquiry frm1) { InitializeComponent(); frm_parent = frm1; } private void btnAdd_Click(object sender, EventArgs e) { connect = 0; try { datconnection.ConnectionString = "server=127.0.0.1;" + "database=as;" + "uid=a;" + "password=a;"; connect = 1; } catch (Exception) { MessageBox.Show("Connection Unavailable"); } //INSERT SQL Code to Insert Venue if (connect == 1) { try { datconnection.Open(); MySqlCommand command = new MySqlCommand("INSERT INTO venues (venue_name,venue_address1,venue_address2,venue_town,venue_county,venue_postcode,venue_telephone,venue_fax,venue_email,venue_web,venue_maxguests) VALUES (?,?,?,?,?,?,?,?,?,?,?)", datconnection); command.Parameters.Add("name", MySqlDbType.VarChar, 45, tbName.Text.ToString()); command.Parameters.Add("address1", MySqlDbType.VarChar, 45, tbAddress1.Text.ToString()); command.Parameters.Add("address2", MySqlDbType.VarChar, 45, tbAddress2.Text.ToString()); command.Parameters.Add("town", MySqlDbType.VarChar, 45, tbTown.Text.ToString()); command.Parameters.Add("county", MySqlDbType.VarChar, 45, tbCounty.Text.ToString()); command.Parameters.Add("postcode", MySqlDbType.VarChar, 10, tbPostcode.Text.ToString()); command.Parameters.Add("telephone", MySqlDbType.VarChar, 15, tbTelephone.Text.ToString()); command.Parameters.Add("fax", MySqlDbType.VarChar, 15, tbFax.Text.ToString()); command.Parameters.Add("email", MySqlDbType.VarChar, 255, tbEmail.Text.ToString()); command.Parameters.Add("web", MySqlDbType.VarChar, 255, tbWeb.Text.ToString()); command.Parameters.Add("maxguests", MySqlDbType.Int32, 11, nudNoOfGuests.Value.ToString()); command.ExecuteNonQuery(); //datdata.InsertCommand = command; } catch (Exception eea) { MessageBox.Show("Error storing data"); MessageBox.Show(eea.ToString()); connect = 0; } finally { datconnection.Close(); } } if (connect == 1) { MessageBox.Show("Data Saved"); //Print //Close Window frm_parent.reloadVenue(); this.Close(); } } 

This gives me a System.FormatException error: the index (based on zero) must be greater than or equal to zero and less than the size of the argument list.

0
source share
2 answers

It appears that some data is larger than the size of the parameter you specified. eg

 command.Parameters.Add("name", MySqlDbType.VarChar, 45, tbName.Text); 

- tbName.Text line size is less than 45. How about using

 command.Parameters.Add("name", MySqlDbType.VarChar, tbName.Text.Length, tbName.Text); 
+2
source

You do not need ToString () in the Text property, this is already a string, please avoid this.

Besides

 command.Parameters.Add("maxguests", MySqlDbType.Int32, 11, nudNoOfGuests.Value.ToString()) 

This expects the int parameter, and you pass the string, use Convert.ToInt32 to pass the value.

Typically, these errors occur due to differences in the number of declared parameters and are passed to the command, but in your case it seems fine. What type of control nudNoOfGuests can cause a format error

+1
source

Source: https://habr.com/ru/post/1392218/


All Articles