Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("exec insertMission @fonction,@service,@missionObj,@excpecteRslt,@startDate,@returnDate,@District_code,@codeFinance,@empno,
@transNum", con);
            cmd.Parameters.AddWithValue("@fonction",txtFonction.Text);
            cmd.Parameters.AddWithValue("@service",txtService.Text);
            cmd.Parameters.AddWithValue("@missionObj",txtObject.Text);
            cmd.Parameters.AddWithValue("@excpecteRslt",txtResult.Text);
            cmd.Parameters.AddWithValue("@startDate",txtStartDate.Text);
            cmd.Parameters.AddWithValue("@returnDate",txtEndDate.Text);
            cmd.Parameters.AddWithValue("@District_code",DropDownListDistr.SelectedValue);
            cmd.Parameters.AddWithValue("@codeFinance",DropDownListFinance.SelectedValue);
            cmd.Parameters.AddWithValue("@empno", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("@transNum",DropDownListTrans.SelectedValue);           
            con.Close();
            con.Open();
            cmd.ExecuteNonQuery();
        }
Posted
Updated 13-Feb-13 7:39am
v2
Comments
ZurdoDev 13-Feb-13 13:48pm    
Which line causes it? It's possible that SelectedValue on a dropdown is null, likely because of an empty dropdown.
Gilbertinino 13-Feb-13 13:51pm    
cmd.Parameters.AddWithValue("@codeFinance",DropDownListFinance.SelectedValue);
error is on this line
ZurdoDev 13-Feb-13 13:55pm    
So, the selectedvalue is null. You need to handle that in the code.
Sergey Alexandrovich Kryukov 12-Mar-13 15:40pm    
As I can see now, a re-post. This is another abuse.
Please, do you ever read what people advise you?
—SA

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
Comments
Maciej Los 13-Feb-13 16:37pm    
BTW, your answer deserve for 5!
Sergey Alexandrovich Kryukov 13-Feb-13 16:49pm    
Oh, thank you, Maciej.
—SA
You already received an explanation in am getting this error while am running this code below "Object reference not set to an instance of an object."[^] (your previous question). If you do not understand this message then you really need to get your study guides out again, and learn about references in .NET.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Feb-13 15:56pm    
A fair point, actually. My 5. As I say, this problem is one of the easiest to spot and fix.
—SA
fjdiewornncalwe 13-Feb-13 16:03pm    
My 5
Maciej Los 13-Feb-13 16:41pm    
+5!
Read this[^] and this[^]. There you'll find the an answer.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Feb-13 14:32pm    
I'm afraid both posts are not concentrated enough on the nature of the issue, cloud the issue with example not very much relevant application-specific detail.
I have my simple way of explaining this simple matter. Please see my answer. What do you think?
—SA
Maciej Los 13-Feb-13 14:42pm    
In Poland we said: Jakie pytanie, taka odpowiedź. I'll try to translate it as good as i can: "An answer is as good as a question".
Sergey Alexandrovich Kryukov 13-Feb-13 14:51pm    
I immediately understood what Polish saying means, even though my knowledge in Slavic languages is limited. (Some people who learned some 3 different Slavic languages, such as Russian, Ukrainian and Belorussian, claimed that that could understand any other Slavic language pretty well...)
—SA
Maciej Los 13-Feb-13 15:27pm    
I'am a man who learned only Russian language from Slavic languages (and of course Polish as a mother tongue). Congrats for knowledge of 3 Slavic languages!
Sergey Alexandrovich Kryukov 13-Feb-13 15:55pm    
You probably did not understand: this is not about me... I know very many words, due to general linguistic experience and interest, but don't really understand the speech.
Interestingly, the how quickly understanding grows: I used to watch Ukrainian TV via youtube, and started to understand more. Many people there speak a lot in Russian though, dialects or something pretty close to "good" Russian (at least on TV), but "official" speech is supposed to be in Ukraine. We watched "Ukraine mae talant" (well, you understand), this program exist in very many countries, but in Ukraine it's especially nice, much better than average, I think...
—SA

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