Ad

Failed To Convert Parameter Value From A ListItem To A String

- 1 answer

I am trying to insert certain values into the table using SQL server 2005. I have used textbox, label and listbox. But by doing so, I have an error that states "Failed to convert parameter value from a ListItem to a String"

How do i solve this?

try
        {
            //Create an insert command
            SqlCommand insertCmd = new SqlCommand("INSERT INTO Account(CustID, Name, TelExtension, Email, Password, AccountType, Status) VALUES " + "(@StaffID, @Name, @TelExtension, @Email, @Password, @StaffType, @Status); SELECT * FROM StaffAccount", sqlCon);

        //Add the parameters to the command
        insertCmd.Parameters.Add("@CustID", SqlDbType.VarChar, 20);
        insertCmd.Parameters.Add("@Name", SqlDbType.VarChar, 50);
        insertCmd.Parameters.Add("@TelExtension", SqlDbType.NChar, 20);
        insertCmd.Parameters.Add("@Email", SqlDbType.VarChar, 50);
        insertCmd.Parameters.Add("@Password", SqlDbType.VarChar, 25);
        insertCmd.Parameters.Add("@AccountType", SqlDbType.VarChar, 2);
        insertCmd.Parameters.Add("@Status", SqlDbType.VarChar, 1);

        //Set the parameters
        insertCmd.Parameters["@CustID"].Value = StaffID_txt.Text;
        insertCmd.Parameters["@Name"].Value = Name_txt.Text;
        insertCmd.Parameters["@TelExtension"].Value = telExtension_txt.Text;
        insertCmd.Parameters["@Email"].Value = email_txt.Text;
        insertCmd.Parameters["@Password"].Value = password_txt.Text;
        insertCmd.Parameters["@AccountType"].Value = StaffType_LB.SelectedItem;
        insertCmd.Parameters["@Status"].Value = status_lblvalue.Text;
Ad

Answer

change

insertCmd.Parameters["@AccountType"].Value = StaffType_LB.SelectedItem;

to

insertCmd.Parameters["@AccountType"].Value = StaffType_LB.SelectedItem.Text;
Ad
source: stackoverflow.com
Ad