my update functions does not work

Hi everyone,
I am having trouble updating information into a form. My saving works fine and retrieving just fine.
I have a store procedure that works this way. If a records exits, update, if it does not exist save a new record.
I tested the store procedure by running it and it works fine if i enter all the variables. If i do not set all variables to Null it wont work. Because of this, on the website i added the following code.
            if (!IsEmpty(TaskAss1.Text))
                cmd.Parameters.Add(new SqlParameter("@TaskAss1",  TaskAss1.Text.Trim()));
            else
                cmd.Parameters.Add(new SqlParameter("@TaskAss1", DBNull.Value));

basically to enter null if there is no value on the textbox. Still does not update. I don't know what else do to.
I do not receive any error on the website when i try to update, but i see that the data has not been update.

any suggestion , will be very appreciated
shpresaAsked:
Who is Participating?
 
shpresaAuthor Commented:
Hi Everyone,

It seemed that i had to clear up my cashe to have the update function to work. Everything else was fine.
0
 
shpresaAuthor Commented:
I also just tried this for all my fields
cmd.Parameters.Add(new SqlParameter("@TaskAss1", DBNull.Value));

updating basing to null values and it worked. SO to me sounds like a null problem. Looks like i m not saving correctly on the webpage.

Any suggestion?
0
 
Brendt HessSenior DBACommented:
If your parameters on the stored procedure can be NULL, it is possible to change the parameters to an optional parameter. So, if your procedure looked like:

CREATE PROCEDURE MyUpdate 
    @param1 varchar(20), 
    @param2 int, 
    @ param3 datetime
AS
....

Open in new window

... and the last two parameters were optional, you could rewrite the definition as :

CREATE PROCEDURE MyUpdate 
    @param1 varchar(20), 
    @param2 int = NULL, 
    @ param3 datetime = NULL
AS
....

Open in new window


Now, if you don't pass in an optional parameter, the parameter will be set to NULL by default, instead of generating an error, so this call is legal:

EXEC MyUpdate @param1='bob'

Open in new window

as is this:

EXEC MyUpdate @param1='bob', @param3='20150101'

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
shpresaAuthor Commented:
I tried placing the variable = null and it is still the same, does not update.
it seems to me that when i check for emty
         if (!IsEmpty(Challenges1.Text))
                cmd.Parameters.Add(new SqlParameter("@Challenges1", Challenges1.Text.Trim()));
            else
                cmd.Parameters.Add(new SqlParameter("@Challenges1", DBNull.Value));
 
it does not pass at all, it does not read it. seems like there is something wrong weith the above lines of code.
Anytime, i place a value
  cmd.Parameters.Add(new SqlParameter("@Challenges1", "test"));
it works.
0
 
sammySeltzerCommented:
It will be nice to show more code but try this:

if (Challenges1.Text==null) {
    cmd.Parameters("@Challenges1").Value = DBNull.Value;
} else {
    cmd.Parameters("@Challenges1").Value = Challenges1.Text;
}

Open in new window


My laptop was acting up I didn't finish my thoughts.

Also, make sure that your asp.net code uses same parameter name as your stored proc.

Finally, you probably need to use Try..Catch to help with the actual problem.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you post your SP definition?
0
 
shpresaAuthor Commented:
that was the solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.