无法将值NULL插入列“但我的列可以为Nullable ... [英] Cannot insert the value NULL into column ” but my column is Nullable…

查看:94
本文介绍了无法将值NULL插入列“但我的列可以为Nullable ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#的新手。我正在创建有6列的数据表(sql)。这是我的表定义



spiceID varchar不允许null(主键)

spiceName文本不允许null

mfddate日期时间不允许空

expdate日期时间不允许空

storeNo varchar不允许null

storeamount float不允许null

priceper1kg float不允许null





这里我不允许nulls.But如果我没有向上面的列插入值,它将不会显示任何错误信息并接受我的内容。

plz帮助我快速解决这个问题br />


这是我的代码。我正在创建商店保管数据项目





使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Text;

usin g System.Windows.Forms;

使用System.Data.SqlClient;



命名空间Ruhunu_spices_PVT_LTD

{

public partial class产品:表格

{

SqlConnection conn1 = new SqlConnection(Data Source = .\\SQLEXPRESS; AttachDbFilename = C :\\Documents and Settings \\Sachintha \\Desktop \\Ruhunu spices(PVT)LTD \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ; User Instance = True);

公共产品()

{

InitializeComponent();

}



private void Product_Load(object sender,EventArgs e)

{



}



private void button6_Click(object sender,EventArgs e)

{

this.Hide() ;



login_view f2 = new login_view();

f2.ShowDialog();



}



private void button1_Click (对象发送者,EventArgs e)

{

SqlCommand comm1 = new SqlCommand(INSERT INTO productT(spiceID,spiceName,MFDdate,EXPdate,storeNo,storeAmount,price1kg)值(@ textBox1,@ comboBox1,'+ dateTimePicker1.Value.Date +','+ dateTimePicker2.Value.Date +',@ textBox2,@ textBox3,@ textBox4),conn1);



comm1.Parameters.Add(@ textBox1,textBox1.Text);

comm1.Parameters.Add(@ comboBox1,comboBox1.Text) ;

comm1.Parameters.Add(@ textBox2,textBox2.Text);

comm1.Parameters.Add(@ textBox3,textBox3.Text);

comm1.Parameters.Add(@ textBox4,textBox4.Text);









conn1.Open();

comm1.ExecuteNonQuery();

MessageBox.Show(插入一行);



textBox1.Clear();

textBox2.Clear();

comboBox1.Text =;

textBox3.Clear();

textBox4.Clear();

dateTimePicker1。 Value = DateTime.Today;

dateTimePicker2.Value = DateTime.Today;





conn1.Close( );



}



private void button5_Click(object sender,EventArgs e)

{

String commandstring;

commandstring = string.Format(SELECT * FROM productT WHERE spiceID ='{0}',textBox1.Text.Trim( ));

SqlDataReader dr1 = null;

SqlCommand com2 = new SqlCommand(commandstring,conn1);

try

{

conn1.Open();

}

catch

{

MessageBox .Show(seaching中的错误,消息,MessageBoxButtons.OKCancel,MessageBoxIcon.Error);

}

dr1 = com2.ExecuteReader();





if(dr1.Read())

{

comboBox1.Text = dr1 [1] .ToString();

dateTimePicker1.Value =(DateTime)dr1 [2];

dateTimePicker2.Value =(DateTime)dr1 [3];

textBox2.Text = dr1 [4] .ToString();

textBox3.Text = dr1 [5] .ToString();

textBox4 .Text = dr1 [6] .ToString();















}

else

{

MessageBox.Show(找不到记录);

}

dr1.Close();

conn1.Close();



}

}

}

I'm newer to c#.I'm creating datatable(sql) which has 6 columns.Here is my table definition

spiceID varchar not allow null (primary key)
spiceName text not allow null
mfddate Datetime not allow null
expdate Datetime not allow null
storeNo varchar not allow null
storeamount float not allow null
priceper1kg float not allow null


here I not allow nulls.But If i not insert value to above columns it won't show any error msg & accept what i entre.
plz help me to solve this quickly

Here is my code.I'm creating store keeping data project


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Ruhunu_spices_PVT_LTD
{
public partial class Product : Form
{
SqlConnection conn1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Sachintha\\Desktop\\Ruhunu spices(PVT)LTD\\Ruhunu spices(PVT)LTD\\Rspices.mdf;Integrated Security=True;User Instance=True");
public Product()
{
InitializeComponent();
}

private void Product_Load(object sender, EventArgs e)
{

}

private void button6_Click(object sender, EventArgs e)
{
this.Hide();

login_view f2 = new login_view();
f2.ShowDialog();

}

private void button1_Click(object sender, EventArgs e)
{
SqlCommand comm1 = new SqlCommand("INSERT INTO productT(spiceID,spiceName,MFDdate,EXPdate,storeNo,storeAmount,price1kg)Values(@textBox1,@comboBox1,'" + dateTimePicker1.Value.Date + "','" + dateTimePicker2.Value.Date + "',@textBox2,@textBox3,@textBox4)", conn1);

comm1.Parameters.Add("@textBox1", textBox1.Text);
comm1.Parameters.Add("@comboBox1", comboBox1.Text);
comm1.Parameters.Add("@textBox2", textBox2.Text);
comm1.Parameters.Add("@textBox3", textBox3.Text);
comm1.Parameters.Add("@textBox4", textBox4.Text);




conn1.Open();
comm1.ExecuteNonQuery();
MessageBox.Show("one row inserted");

textBox1.Clear();
textBox2.Clear();
comboBox1.Text = "";
textBox3.Clear();
textBox4.Clear();
dateTimePicker1.Value = DateTime.Today;
dateTimePicker2.Value = DateTime.Today;


conn1.Close();

}

private void button5_Click(object sender, EventArgs e)
{
String commandstring;
commandstring =string.Format( "SELECT * FROM productT WHERE spiceID='{0}'", textBox1.Text.Trim());
SqlDataReader dr1 = null;
SqlCommand com2 = new SqlCommand(commandstring, conn1);
try
{
conn1.Open();
}
catch
{
MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
dr1 = com2.ExecuteReader();


if (dr1.Read())
{
comboBox1.Text = dr1[1].ToString();
dateTimePicker1.Value = (DateTime)dr1[2];
dateTimePicker2.Value = (DateTime)dr1[3];
textBox2.Text = dr1[4].ToString();
textBox3.Text = dr1[5].ToString();
textBox4.Text = dr1[6].ToString();







}
else
{
MessageBox.Show("Record can not found");
}
dr1.Close();
conn1.Close();

}
}
}

推荐答案

我猜它插入空字符串。如果你想插入null,你需要检查控件是否为空,然后显式传递DBNull。
I guess its inserting empty string. If you want to insert null you need to check if the controls is empty then explicitly pass DBNull.


好吧,因为你没有提供你用来设置这个<$的代码c $ c> null value并将其写入数据库...



A null 是C#与数据库null或DbNull不同。如果没有看到你的代码,就不可能在没有盲目猜测的情况下告诉你如何修复它。
Well, since you didn't provide the code you're using to set this null value and write it to the database...

A null is C# is not the same a database null, or DbNull. Without seeing your code, it's impossible to tell you how to fix it without blindly guessing.


这篇关于无法将值NULL插入列“但我的列可以为Nullable ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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