Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server
 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 3-Feb-13 6:47am
Edited 3-Feb-13 20:30pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
selva_1990 at 3-Feb-13 12:55pm
   
i ticked allow null in table definition still error... any way to stop that
milenalukic at 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
good
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.
  Permalink  
Comments
selva_1990 at 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
0 OriginalGriff 239
1 Sarvesh Kumar Gupta 185
2 George Jonsson 154
3 ArunRajendra 144
4 Richard MacCutchan 140


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid