Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am sending the text box values as parameters into stored procedure which corresponds to varchar, datetime, int types of fields in database. I have converted all of them to their respective types. Some textboxes are blank , I mean while sending parameters null values are being passed. But I have already ON the Allow nulls field and mentioned the same in stored procedure in name of default values. What should I do?


I think the main issue is with null values which I think are not getting converted to string but i am unable to to fix it.Pls help asap

What I have tried:

I have used textbox.text.toString(), and crosschecked all the parameters.
Posted
Updated 22-Dec-16 19:18pm
Comments
[no name] 22-Dec-16 8:13am    
When asking for help on your code, it usually beneficial to SHOW us your code. null in C# is not the same as the null value in your database.
"I have used textbox.text.toString()", uh huh.... first thing is that line of code would not compile and what kind of benefit do you think calling ToString on a property that is already a string would be?
Richa Gupta 23-Dec-16 1:19am    
please see the code i have posted below to understand my problem better.
Richa Gupta 26-Dec-16 1:15am    
ok sir I got your point but what is the solution to this problem now. I know its like trying to convert a blank into string.How can I send a null value to database then.

Before you start talking to SQL, use the various TryParse methods to convert the user textbox inputs to native datatypes (int, float, DateTime for example) and report any problems back to the user.
Once you have "good" native versions, use a parameterised query to pass the native types to SQL - converting null to DbNull.Value where both your database columns and your definition of the SP parameters allow NULL values.
 
Share this answer
 
Comments
Richa Gupta 23-Dec-16 1:19am    
please see the code i have posted below to understand my problem better.
Yes, here is my stored procedure:
SQL
ALTER PROCEDURE [dbo].[sp_CRUDempDetail]
@empcode int,
@gender nchar(10)=null,
@dob date=null,
@fhname varchar(50)=null,
@Nationality varchar(50)=null,
@marital varchar(30)=null,
@bgroup nchar(10)=null,
@email varchar(50)=null,
@mobno varchar(50)=null,
@altmobno varchar(50)=null,
@corr_add varchar(150)=null,
@corr_city varchar(80)=null,
@corr_state varchar(80)=null,
@corr_pin_code int=null,
@corr_country varchar(80)=null,
@pmt_add varchar(150)=null,
@pmt_city varchar(80)=null,
@pmt_state varchar(80)=null,
@pmt_pin_code int=0,
@pmt_country varchar(80)=null,
@status varchar(50)=null
AS
BEGIN
SET NOCOUNT ON;
--- Insert details of Employee
IF @status='InsertEmpDetails'
BEGIN
UPDATE tblEmployee SET Gender=@gender,DOB=@dob,Father_Husband_Name=@fhname,Nationality=@Nationality,
Marital_Status=@marital,Blood_Group=@bgroup,Email=@email,Mob_No=@mobno,Alt_Mob_No=@altmobno,Corr_Address=@corr_add,
Corr_City=@corr_city,Corr_Country=@corr_country,Corr_PinCode=@corr_pin_code,Corr_State=@corr_state,Pmt_Address=@pmt_add,
Pmt_City=@pmt_city,Pmt_Country=@pmt_country,Pmt_PinCode=@pmt_pin_code,Pmt_State=@pmt_state
WHERE Emp_Code=@empcode
END

--- Select Records in Table
IF @status='SELECT'
BEGIN
SELECT * FROM tblEmployee
END
End



and here is my c# code:


C#
protected void btnSave_Click(object sender, EventArgs e)
    {
        using (DataClassesDataContext ctx = new DataClassesDataContext())
        {
            var result = ctx.sp_CRUDempDetail(Convert.ToInt32(lblemp_code.InnerText),rdlGender.SelectedValue,Convert.ToDateTime(txtDOB.Text),txtFHname.Text.ToString(),txtNation.Text.ToString(),rdlmarital.SelectedValue,txtBloodGroup.Text.ToString(),txtemail.Text.ToString(),txtmob.Text.ToString(),txtMob2.Text.ToString(),txtcorAdd1.Text.ToString(),txtcorCity.Text.ToString(),txtcorState.Text.ToString(), Convert.ToInt32(txtcorpin.Text),txtcorcountry.Text.ToString(), txtPmtAdd1.Text.ToString(), txtPmtCity.Text.ToString(), txtPmtState.Text.ToString(), Convert.ToInt32(txtPmtpin.Text), txtPmtcountry.Text.ToString(), "InsertFamilyDetails");
            Message("Saved Successfully!");
            Clear();
        }
    }
 
Share this answer
 
can you put the sql code and C# call code.
about null values if don't send null from C#, just search about DBNull.
 
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