when i write this code then it shows error...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kplshrm7
    New Member
    • Aug 2010
    • 3

    when i write this code then it shows error...

    Error:- Failed to convert parameter value from a String to a Int32.

    code--------------------
    Code:
    SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader reader;
            string connectionString = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
            conn = new SqlConnection(connectionString);
            cmd = new SqlCommand("select Username, Address, City, State, " + "HomePhone, Extention, MobilePhone from employees" + "where Name=@Name", conn);
            cmd.Parameters.Add("@Name",System.Data.SqlDbType.Int);
            cmd.Parameters["@Name"].Value= DropDownList1.SelectedItem.Value.ToString();
            try
            {
                conn.Open();
          reader=cmd.ExecuteReader();
                        {
                    nametextbox.Text = reader["Name"].ToString();
                    usernameTextBox.Text = reader["Username"].ToString();
                    addressTextBox.Text = reader["Address"].ToString();
                    cityTextBox.Text = reader["City"].ToString();
                    stateTextBox.Text = reader["State"].ToString();
                    //zipTextBox.Text = reader["Zip"].ToString();
                    homephoneTextBox.Text = reader["HomePhone"].ToString();
                    extentionTextBox.Text = reader["Extention"].ToString();
                    mobileTextBox.Text = reader["MobilePhone"].ToString();
                }
                reader.Close();
                updateButton.Enabled = true;
            }
            /*catch
            {
                Label1.Text = "Error!!!!!!";
            }*/
            finally
            {
    
                conn.Close();
            }
    Last edited by Frinavale; Aug 11 '10, 04:27 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    On line 7 & 8 in the above posted code you have the following:
    Code:
    cmd.Parameters.Add("@Name",System.Data.SqlDbType.Int);
    cmd.Parameters["@Name"].Value= DropDownList1.SelectedItem.Value.ToString()
    You are stating that the @Name parameter should be an "SqlDbType. Int" but then you try to supply a String as a value.

    Change the "SqlDbType. Int" to an "SqlDbType.VarC har" if your database is expecting a String. If your database is expecting an Int value for the name then provide an integer (not a String) as a parameter.

    -Frinny

    Comment

    • benwizzle
      New Member
      • May 2010
      • 72

      #3
      If I'm correct, you are not converting the value retrieved from the dropdownlist into an int. @Name is an Int32 and the value retrieved from DropDownList1 is a string try this
      Code:
      cmd.Parameters["@Name"].Value = Convert.ToInt32(DropDownList1.SelectedItem.Value);

      Comment

      Working...