Click here to Skip to main content
13,251,998 members (59,684 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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);
                    int rows = command.ExecuteNonQuery();

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

i get the error :

string or binary data would be truncated

any help appreciated

Posted 8-May-12 7:56am
Updated 8-May-12 8:29am

1 solution

Rate this: bad
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,

Should be ddlcompany.SelectedIndex or ddlcompany.SelectedValue because SelectedItem is referencing the ListItem itself, not a value.
BBCokeley 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?

BBCokeley 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 8 May 2012
Copyright © CodeProject, 1999-2017
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