Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey, im trying to parse information from a datagrid into an sql table

this is what im thinking:

C#
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
Updated 8-May-12 7:29am
v3

1 solution

Usually, the reported error raises when you are selecting 'SelectedIndex' from the collection. As an example,
C#
cmd.Parameters.AddWithValue("@company",ddlcompany.SelectedItem);


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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900