title: Error : Object must implement IConvertible
body: <p>I am trying to insert Listbox Items and a Textbox value for each of the listbox items to the database when I get the below error.</p>
  
  <p>IF i try to insert the list box items only I am successful but when i try to insert the textbox value I get this error. Can you please tell me what I am doing wrong.</p>
  
  <pre>
  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: 
  </pre>
  
  <hr>
  
  <p>c# CODE</p>
  
  <pre><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 &lt; 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);
      }
  </code></pre>
  
  <hr>
  
  <p>I want to add all the values of the listbox to the database. </p>
  
  <p>Secondlly even if I try to use .</p>
  
  <p>SelectedItem</p>
  
  <p>then I get the following error.</p>
  
  <pre>
  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'.
  </pre>
  
  <p>Any idea where I am going wrong?</p>