相关文章推荐
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) 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); 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. 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.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •  
    推荐文章