We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,420 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

please check the code.

hello!
i am facing an error ("object refrence is not set to an instance of an object.") i check my all code but i can able to solve it . here is a line of code where i getting it.

.
.
.
cmd = new sqlcommand(_mycommand_)

if (__mycondition)
{
serialNo = convert.ToInt16(cmd.executeScalar().toString());//----here i got an error
// as serialNo is a variable defined publicaly in my struct and datatype of it is int
}

please help me in this , so that i can move forword.

Best regards

6
Contributors
5
Replies
19 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
M.Waqas Aslam
Master Poster
748 posts since Aug 2011
Reputation Points: 50
Solved Threads: 121
Skill Endorsements: 2

put .ExecuteScalar instead of .executeScalar (use a capital E)

Why are you doing:

convert.ToInt16(cmd.executeScalar().toString());

take off the .toString() (plus its To.String()), convert it straight to string and put a capital C on convert.

Hope this helps

ChrisHunter
Posting Pro
566 posts since Feb 2011
Reputation Points: 104
Solved Threads: 51
Skill Endorsements: 13

"object refrence is not set to an instance of an object."

Cause 1:
Not declaring variables!

Yes, I know it sounds obvious, but MAKE SURE that you've explicity declared the variable, and don't forget to use the appropriate scope!

Cause 2:
Bad scoping

public Form1()
{
    private string strHello;
}


 private void btnOpen_Click(object sender, EventArgs e)
        {            
            strHello = "Hello";
            Textbox.Text = strHello;
        }

This won't work because strHello is only accessable in Form1()

If anybody knows more please let me know, thanks.

Michael27
Junior Poster in Training
93 posts since Jul 2010
Reputation Points: 23
Solved Threads: 6
Skill Endorsements: 2

You need to make sure your command is actually returning some data. There is a pretty good chance that it isn't (hence the null reference exception caused by trying to convert a null reference to an int16).

If this is the case, I recommend checking for nulls and having a fall-back default value or some type of exception handling in place.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

If your SQL is returning something that cannot be converted, that can be a problem.
What was the exact error you received?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

Instantiate your struct which has serial no as a variable and then access the serial number using the object of that struct. the struct has not been initialised, hence you are getting "object refrence is not set to an instance of an object." exception. try this out.

arunkumars
Posting Whiz in Training
204 posts since Jul 2009
Reputation Points: 26
Solved Threads: 22
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0759 seconds using 2.67MB