Click here to Skip to main content
12,625,694 members (36,964 online)
Rate this:
 
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
Updated 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 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 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 | Mobile
Web01 | 2.8.161205.3 | Last Updated 8 May 2012
Copyright © CodeProject, 1999-2016
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