none
SqlException RRS feed

  • Question

  • My code looks like this:

    List<string> items;

    private void button2_Click(object sender, EventArgs e)

    {

    //finalize button click

    items = new List<string>();

    for (int i = 0; i < passagelistBox.Items.Count; i++)

    {

    items.Add(passagelistBox.ItemsIdea.ToString());

    }

    foreach (string currentitem in items)

    {

    DoInsertData(currentitem.Split(new char[] {','}));

    }

    }

    private void DoInsertData(string[] item)

    {

    string sql = "INSERT INTO tblPassages (column_num, row_num) VALUES ('"+item[0]+"','"+item[1]+"')";

    cmd.CommandText = sql;

    //SqlParameter p1 = new SqlParameter("@p1", item[0]);

    //SqlParameter p2 = new SqlParameter("@p2", item[1]);

    //cmd.Parameters.Add(p1);

    //cmd.Parameters.Add(p2);

    //p1.SqlDbType = SqlDbType.VarChar;

    //p2.SqlDbType = SqlDbType.VarChar;

    cmd.Connection = cn;

    cn.Open();

    cmd.ExecuteNonQuery();

    cn.Close();

    cmd.Connection = null;

    }

    But at cmd.ExecuteNonQuery(); I get an exception saying:

    System.Data.SqlClient.SqlException was unhandled
      Message="String or binary data would be truncated.\r\nThe statement has been terminated."
      Source=".Net SqlClient Data Provider"
      ErrorCode=-2146232060.

     

    How to avoid this???

     

    *This post may be at a wrong thread, as I am congused.

     

    Tuesday, July 17, 2007 5:02 AM

Answers

  • hi,

     

    check your database design. I think the Column to which you are inserting the value has less space to insert.

     

    check it out.

     

    Tuesday, July 17, 2007 5:10 AM

All replies

  • hi,

     

    check your database design. I think the Column to which you are inserting the value has less space to insert.

     

    check it out.

     

    Tuesday, July 17, 2007 5:10 AM
  • Thanx....

     

    1. But this stores data in the table as :

    col1                       col2

    {X=11                  Y=4}

    {X=11                  Y=5}

    .

    .

    .

     

    I want the data should be stored as

    col1                       col2

       11                         4

       11                         5

    ...

    ..

     

     2. I want the row to be unique should I make both the columns Composite Primary Key.

     

    Waitng for ur expert opinion......

    Tuesday, July 17, 2007 5:33 AM
  • Hi,

     

    1. I think the problem is in your code. Thats why the data is stored like X=10.

        Change the Column data-type to Numeric so that it can accept only numeric values.

     

    2. You can use both the columns combined to have a Primary key.(this is based on your requirement)

     

     

    Regards,

    Perumal.R

    Wednesday, July 18, 2007 10:18 AM