我正在尝试使用SQL Server 2005将某些值插入表中。 我使用过textbox,label和listbox。 但通过这样做,我有一个错误,指出“无法将参数值从ListItem转换为字符串”
我如何解决这个问题?
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;
答案 0 :(得分:4)
变化
insertCmd.Parameters["@AccountType"].Value = StaffType_LB.SelectedItem;
到
insertCmd.Parameters["@AccountType"].Value = StaffType_LB.SelectedItem.Text;