hMp
hMp

Reputation: 27

Struggling to populate datagrid from stored procedure with parameters

protected DataTable RetrieveAlumni2()
        {
            {
                MySqlConnection con = new MySqlConnection("server=100.0.0.0;user id=id;password=pass;database=db;persistsecurityinfo=True");
                MySqlCommand cmd = new MySqlCommand("get_alumni_by_city", con);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                try
                {
                    string city = textBox1.Text;
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@city", SqlDbType.VarChar).Value = city;
                    da.SelectCommand = cmd;
                    cmd.ExecuteNonQuery();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    cmd.Dispose();
                    con.Close();
                }
                return dt;
            }
        }

Giving error:

"input string was not in a correct format"

City is varchar in the mysql server. Any help would be appreciated.

Upvotes: 1

Views: 200

Answers (2)

Mad Dog Tannen
Mad Dog Tannen

Reputation: 7244

Have you tried just this?

cmd.Parameters.AddWithValue("@city","" + city + "");

Upvotes: 1

Setherith
Setherith

Reputation: 329

Check the type of @city in your stored procedure. If it doesn't match what your throwing in, it will reject it.

Upvotes: 0

Related Questions