Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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)
                       );

               try
               {
                   conn.Open();
                   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("@Data",data);
                   cmd.Parameters.AddWithValue("@Date", DateTime.Now);
                   cmd.ExecuteNonQuery();
                   long si;
                   si = fi.Length / 1024;
                   MessageBox.Show("u inserted the file size of " + si.ToString() + "kb");
                   textileDataSet.Tables[0].Rows.Add(fi.Name,data);
                   textileDataSet.Tables[0].AcceptChanges();
                   conn.Close();
               }
               catch (Exception r)
               {
                   MessageBox.Show(r.Message);
               }
           }


       }





while adding second time its throwing error
Posted
Updated 3-Feb-13 20:30pm
v7

Hi,

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.
 
Share this answer
 
Comments
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.
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.
 
Share this answer
 
Comments
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


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