Click here to Skip to main content
13,596,080 members
Rate this:
Please Sign up or sign in to vote.
See more:
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
           if (openFileDialog1.ShowDialog() == DialogResult.OK)

               FileInfo fi = new FileInfo(openFileDialog1.FileName);
               byte[] data = new byte[fi.Length];
               FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
               fs.Position = 0;
               fs.Read(data, 0, Convert.ToInt32(fi.Length));
               SqlConnection conn = new SqlConnection(string.Format("Data Source=SELVA-T;Initial Catalog=textile;Integrated Security=True"
                       , ServerName
                       , DatabaseName)

                   SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],Date1) VALUES (@File,@Data, @Date)", conn);
                   cmd.Parameters.AddWithValue("@File", fi.Name);
                   cmd.Parameters.AddWithValue("@Date", DateTime.Now);
                   long si;
                   si = fi.Length / 1024;
                   MessageBox.Show("u inserted the file size of " + si.ToString() + "kb");
               catch (Exception r)


while adding second time its throwing error
Posted 3-Feb-13 6:47am
Updated 3-Feb-13 20:30pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


The error suggests that the table has a field that does not allow null values.

Either one of your variables is null or there other fields in the table that do not allow nulls and do not have a default value set.

You can try running the insert statement direct form SQL management studio to determine if your error lies in the c# code or the statement itself.
selva_1990 3-Feb-13 12:55pm
i ticked allow null in table definition still error... any way to stop that
milenalukic 3-Feb-13 13:02pm
Try running the insert statement from SQL. If you still get an error please send create table script and error received to be able to examine further.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This exception occurs when you try to insert a null value into a column where AllowDBNull is set to false.

Based on the source code you have posted here, it looks like either fi.Name or data is null.
Try debugging the code to figure out which value could be null.
selva_1990 3-Feb-13 12:59pm
i changed all column as allow null value...
but its same error

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 | Cookies | Terms of Service
Web03-2016 | 2.8.180621.3 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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