Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ADO.NET
hey, im trying to parse information from a datagrid into an sql table
 
this is what im thinking:
 
private void storeDataBTN_Click(object sender, EventArgs e)
{
    //DataTable BookDetails = new DataTable("BookDetails");
    using (conn)
    {
        SqlCommand command = new SqlCommand("INSERT INTO BookDetails VALUES(@Title, @AuthorFirstName, @AuthorSurname, @AquisitionDate, @PublisherName ,@PublisherCity, @PublishingDate, @ISBN, @DDClassification)", conn);
        foreach (DataGridViewRow dr in DGTables.Rows)
        {
            
                command.Parameters.AddWithValue("@Title", titleDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AuthorFirstName", authorFirstNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AuthorSurname", authorSurnameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AquisitionDate", aquisitionDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue("@PublisherName", publisherNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@PublisherCity", publisherCityDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@PublishingDate", publishingDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue("@ISBN", iSBNDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@DDClassification", dDClassificationDataGridViewTextBoxColumn);
 
                try
                {
                    conn.Open();
                    int rows = command.ExecuteNonQuery();
 

                    if (rows > 0)
                    {
                        MessageBox.Show("Insert succesful");
                    }
 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }            
        }
    }
 

i get the error :
 
string or binary data would be truncated
 
any help appreciated
 
thanks
Posted 8-May-12 7:56am
Edited 8-May-12 8:29am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Usually, the reported error raises when you are selecting 'SelectedIndex' from the collection. As an example,
cmd.Parameters.AddWithValue("@company",ddlcompany.SelectedItem);
 
Should be ddlcompany.SelectedIndex or ddlcompany.SelectedValue because SelectedItem is referencing the ListItem itself, not a value.
  Permalink  
Comments
BBCokeley at 8-May-12 13:42pm
   
ok thanks, changed it to .Selected, just to try, now im getting a more reasonable error:
 
Conversion failed when converting date and/or time from charecter string.
 
any ideas please?
 
thanks
BBCokeley at 8-May-12 14:04pm
   
I dont quite understand, although i know what your getting at, but this is not a dropdownlist, so i cannot use selected item or selected value.
 
im using the above because it is the only available option that makes sense, is there a way of retrieving the data from the rows?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 8 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100