nonullallowedexception未处理c#(列'Bill Name'不允许空值) [英] nonullallowedexception was unhandled c# (Column 'Bill Name' does not allow nulls)

查看:320
本文介绍了nonullallowedexception未处理c#(列'Bill Name'不允许空值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案





错误表明该表有一个不允许空值的字段。



任何一个您的变量是null或表中的其他字段不允许空值并且没有设置默认值。



您可以尝试直接运行insert语句形成SQL管理工作室以确定您的错误是否位于c#代码或语句本身。
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.


当您尝试将空值插入 AllowDBNull 设置为 false



根据您拥有的源代码发布在这里,它看起来像 fi.Name 数据为空。

尝试调试公司de来确定哪个值可以为null。
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.


这篇关于nonullallowedexception未处理c#(列'Bill Name'不允许空值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆