使用storedprocedure从文本框向数据库中插入空值 [英] Inserting null value into database from textbox using storedprocedure

查看:84
本文介绍了使用storedprocedure从文本框向数据库中插入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cscode:

Cscode:

string FIRST_NAME = txtFirstName.Text.Length == 0 ? null : txtFirstName.Text;





SP:



SP:

cmd.Parameters.AddWithValue("@FIRST_NAME", FIRST_NAME);







错误:




Error:

INSERT_NEW_STARTER_DETAILS' expects parameter '@FIRST_NAME', which was not supplied.



我正在尝试在文本框为空时将空值插入数据库。但是当文本框中有文本时SP会插入数据,而当文本框为空时不插入(null)。



有没有其他简单的方法来实现它。



谢谢,

Sathish


I am trying insert null value into database when the textbox is empty. but the SP insert the data when there is text in textbox and not inserting when it is empty (null).

Is there any other easy way to achieve it.

Thanks,
Sathish

推荐答案

您好,



你可以尝试这个,

将null添加为默认参数值,如果你想让它为null则不传递任何值



例如:



在你的sp

Hello,

You can try this,
add null as default paramter value and don't pass any value if you want it to be null

example:

In your Sp
create procedure sp_PassNullValueInSp(@para1 varhcra(10), @para2 varchar(10)= null)





C#代码



C# Code

cmd.Parameters.AddWithValue("@para1", "value1");
//No need to pass para2 if it is null


这篇关于使用storedprocedure从文本框向数据库中插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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