Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used store procedure to insert data but the problem is that it inserts spaces into field when the text box is null. How I solve it! Could anybody please solve it?

Rabindranath
Posted
Comments
BulletVictim 1-Aug-14 0:45am    
IF @TEXT = ''
begin
set @TEXT = NULL
end
INSERT INTO TABLE(TEXT)
values(@TEXT)

or you could just do a check on the c# code
if (textbox1.text.trim() == "" || textbox1.text == string.Empty)
{
textbox1.text = "NULL";
}
call stored procedure here

textbox1.text = "";

Also if you would like to do the check in SQL please remember to pass the textbox.text.trim() value into the stored procedure as to avoid the problem occurring if a the spacebar was pressed within the textbox and no other text was entered
sulomila 1-Aug-14 2:41am    
Thank you very much for this code. I am curious to know that why it happens when i use it store procedure.
BulletVictim 4-Aug-14 1:43am    
As far as I know It is not an issue with stored procedures. This would mainly happen because in your c# code when the textbox's text is empty it is not handled as a "null" value when it is passed to the stored procedure rather (and you would have noticed in debugging) it is handled as "" or sometimes " ". Someone a lot smarter than me will be able to explain to you why this is happens.
I have however made it standard practice when passing a textbox's text to the database to pass it as textbox1.text.trim() to ensure that there are no empty spaces at the start or end of the text, this also eliminates accidental "enter" presses at the end of text in multiline textboxes.
An alternative to the SQL code in my previous comment would be.
declare @TEXT varchar(255) = null
This helps to always make it a true null value if no value has been given to it. But try it in your environment and see if it works for you.

1 solution

u can do

IF(LTRIM(RTRIM(@string))<>'')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900