我的应用程序中的异常和错误 [英] Exception and Erorr in my application

查看:85
本文介绍了我的应用程序中的异常和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经写了这段代码

SqlConnection con = new SqlConnection("Data Source=MOSTAFA;Initial Catalog=mohasba;Integrated Security=True");

SqlCommand com = new SqlCommand("insert into Ezn_Qaid (Qaid_no,Qaid_date,Maden,Dayn,El_Qema,Qaid_Type,Bayan_Oll) values(''" + B_No.Text + "'',''" + B_Date.Text + "'',''" + B_m.Text + "'',''" + B_D.Text + "'',''" + B_Q.Text + "'',''" + comboBox3.SelectedIndex + "'',''" +comboBox1.SelectedIndex+ "'')", con);
            
con.Open();
com.ExecuteNonQuery();



并获得以下erorr

  INSERT 语句与关键字 "  span>  FK__Ezn_Qaid__Bayan ___ 44FF419A".发生冲突 数据库 "  mohasba""   ' .
该语句已终止.



注意
数据库

create table Ezn_Qaid 
( 
Qaid_no nvarchar(20) , 
Qaid_date nvarchar(10)  , 
Maden float , 
Dayn float, 
El_Qema float,
Qaid_Type nvarchar(15) , 
--byan_name int NOT NULL,   -- this is the new column 
Bayan_Oll nvarchar(100) FOREIGN KEY (Bayan_Oll) REFERENCES Bayan(byan_name ),
Bayan_Bank nvarchar(100) FOREIGN KEY (Bayan_Bank ) REFERENCES Bank(byan_name ),
Bayan_3momya nvarchar(100) FOREIGN KEY (Bayan_3momya) REFERENCES msaref_3momya(byan_name ),
Bayan_Tkalef nvarchar(100) FOREIGN KEY (Bayan_Tkalef) REFERENCES tkalef_nshat (byan_name )
) 


create table Bayan
(
byan_no int identity ,
byan_name nvarchar(100)PRIMARY KEY
)


byan_name列仅包含一个记录"Visa",而组合框项目则包含"Visa"

谢谢

解决方案

似乎您的命令字符串不应该使用comboBox1.SelectedIndex,而应该使用comboBox1.SelectedItem. SelectedIndex将为您提供0输出,而不是组合框中文本的字符串.


在史蒂夫所说的顶部,您可能想对Google进行".net参数化查询"并找到找出为什么您用来构建SQL INSERT语句的那堆字符串连接被认为是糟糕的代码.您试图将空值插入到约束为不包含空值的列中.

删除约束或为列添加值.


Hi
I have wrote this code

SqlConnection con = new SqlConnection("Data Source=MOSTAFA;Initial Catalog=mohasba;Integrated Security=True");

SqlCommand com = new SqlCommand("insert into Ezn_Qaid (Qaid_no,Qaid_date,Maden,Dayn,El_Qema,Qaid_Type,Bayan_Oll) values(''" + B_No.Text + "'',''" + B_Date.Text + "'',''" + B_m.Text + "'',''" + B_D.Text + "'',''" + B_Q.Text + "'',''" + comboBox3.SelectedIndex + "'',''" +comboBox1.SelectedIndex+ "'')", con);
            
con.Open();
com.ExecuteNonQuery();



and get the following erorr

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Ezn_Qaid__Bayan___44FF419A". The conflict occurred in database "mohasba", table "dbo.Bayan", column 'byan_name'.
The statement has been terminated.



Note
the database

create table Ezn_Qaid 
( 
Qaid_no nvarchar(20) , 
Qaid_date nvarchar(10)  , 
Maden float , 
Dayn float, 
El_Qema float,
Qaid_Type nvarchar(15) , 
--byan_name int NOT NULL,   -- this is the new column 
Bayan_Oll nvarchar(100) FOREIGN KEY (Bayan_Oll) REFERENCES Bayan(byan_name ),
Bayan_Bank nvarchar(100) FOREIGN KEY (Bayan_Bank ) REFERENCES Bank(byan_name ),
Bayan_3momya nvarchar(100) FOREIGN KEY (Bayan_3momya) REFERENCES msaref_3momya(byan_name ),
Bayan_Tkalef nvarchar(100) FOREIGN KEY (Bayan_Tkalef) REFERENCES tkalef_nshat (byan_name )
) 


create table Bayan
(
byan_no int identity ,
byan_name nvarchar(100)PRIMARY KEY
)


The column byan_name contain only one record "Visa" and the combobox item contain "Visa"

Thanks

解决方案

It seems that your command string should not be using comboBox1.SelectedIndex, but should probably use comboBox1.SelectedItem instead. SelectedIndex will give you a 0 out and not the string from the text in the combobox.


On top of what Steve said, you might want to Google for ".net parameterized queries" and find out why that pile of string concatentations your using to build the SQL INSERT statement is considered terrible code.


In addition to the other fine answers, your insert statement does not include the byan_name column which means that you are trying to insert a null value to a column that is constrained not to contain a null value.

Remove the constraint or add a value for the column.


这篇关于我的应用程序中的异常和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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