I am trying to insert list items and the text box value for each list item into the database when I get the following error.
IF I try to insert list items only I succeed, but when I try to insert the value of a text field, I get this error. Could you tell me what I am doing wrong.
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);
}
I want to add all list values โโto the database.
Secondly, even if I try to use.
SelectedItem
then I get the following error.
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'.
Any idea where I'm wrong?
source
share