当出现以下错误时,我试图将列表框项和每个列表框项的文本框值插入数据库。
如果我只尝试插入列表框项目,我会成功,但是当我尝试插入文本框值时,我会收到此错误。你能告诉我我做错了什么吗?
Error Message: Object must implement IConvertible. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Object must implement IConvertible. Source Error: Line 60: Line 61: Line 62: cmd.ExecuteNonQuery(); Line 63: Line 64:
c# CODE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["RM_Jan2011ConnectionString"].ConnectionString;
}
private void InsertRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "INSERT INTO FileID(File_Code, Dept_Code) VALUES(@File_Code, @Dept_Code)";
sb.AppendFormat("{0}('{1}'); ", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.Parameters.Add("@File_Code", SqlDbType.VarChar);
cmd.Parameters["@File_Code"].Value = ListBox2.Items;
cmd.Parameters.Add("@Dept_Code", SqlDbType.VarChar);
cmd.Parameters["@Dept_Code"].Value = DropDownList1.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert ('Records Successfuly Saved!');", true);
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
int myint = Convert.ToInt32(TextBox1.Text) + 1;
for (int i = 1; i < myint; i++)
{
ListBox2.Items.Add(DropDownList1.SelectedItem.ToString() + i.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
foreach (ListItem item in ListBox2.Items)
{
{
sc.Add(item.Text);
sc.Add(DropDownList1.Text);
}
}
InsertRecords(sc);
}
我想将列表框的所有值添加到数据库中。
其次,即使我尝试使用 .
选定项
然后我得到以下错误。
Insert Error: Incorrect syntax near 'CPD1'. Incorrect syntax near 'CPD'. Incorrect syntax near 'CPD2'. Incorrect syntax near 'CPD'. Incorrect syntax near 'CPD3'. Incorrect syntax near 'CPD'.
知道我哪里出错了吗?
最佳答案
如果您的列表框只包含您需要更改以下行的字符串项
cmd.Parameters["@File_Code"].Value = ListBox2.Items
到
cmd.Parameters["@File_Code"].Value = ListBox2.SelectedItem
如果项目是复杂的对象,在末尾添加 .ToString()
UPD:如果你想添加所有 ListBox 项目,你需要循环遍历其项目集合并为每个项目执行插入查询,如下所示:
foreach(string item in ListBox2.Items)
{
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
cmd.Parameters.Add("@File_Code", SqlDbType.VarChar);
cmd.Parameters["@File_Code"].Value = item;
cmd.Parameters.Add("@Dept_Code", SqlDbType.VarChar);
cmd.Parameters["@Dept_Code"].Value = DropDownList1.Text;
cmd.ExecuteNonQuery();
}
关于c# - 错误 : Object must implement IConvertible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966803/