1,105,320 Community Members

numericupdown.value generates error when converting to string

Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I am inserting an int value using numericupdownvalue from a form to database, using the following code

SqlCommand cmd = new SqlCommand("insert into student where student_no='"+numericupdown1.value+"'",con)

Now I have another form that is when loaded, will read the inserted value made by the numericupdown.value, using the following code

numericUpDown1.Value = dt.Rows[0]["Bookshelf"].ToString();

but it generates an error cannot implicitly convert type 'string' to decimal.

how to fix this?

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

First of all please focus on sql (learn) statements. INSERT statement is completely incorrect.

Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

My bad. Of course, i know how to do it.

I couldn't seem to edit my first post, so here it is.

SqlCommand cmd = new SqlCommand("select * from student where student_no='"+numericupdown1.value+"'",con)
Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

To be more specific here to your other post regarding incorrect data type. Essentially, I'm not sure the specific value of numericupdown1.Value (if it's integer or bool, or double) but you could save yourself a lot of issue if you changed your SELECT statement as such:

SqlCommand cmd = new SqlCommand("select * from student where student_no='" + numericupdown1.value.ToString() + "'",con)

Hope that helps :)

Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I add ToString() in my sql select statement. Then I used this again to read the value from the database.

numericUpDown1.Value = dt.Rows[0]["Bookshelf"].ToString();

The error shows up again.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

Well again, what is the data type for numericUpDown1.Value?

Another thing I'm seeing here (and your way might work but I prefer to do it this way):

numericUpDown1.Value = dt.Rows[0].Columns["Bookshelf"].ToString();

However, if numericUpDown1.Value has datatype decimal then converting dt.Rows[0].Columns["Bookshelf"] to a string value and trying to pass it to a decimal container will fail.

My assumption will be that when it's pulled from the DB it's going to be presented as a decimal value anyway in which case you can drop the .ToString() from this part (keeping it in the SELECT statement) and if absolutely required can add a Convert.ToDecimal(dt.Rows[0].Columns["Bookshelf"]) to ensure it's registering as a decimal prior to trying to apply it to the numericUpDown1.Value variable.

Worst case scenario (ie: my brain hasn't tuned up yet for the day) use:

numericUpDown1.Value = Convert.ToDecimal(dt.Rows[0].Columns["Bookshelf"].ToString());
Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

The datatype I used for Bookshelf in my database is INT, so why is that it returns the value as decimal? datarow doesn't have a definition for column.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

It's probably returning the value as INT, but your numericUpDown1.Value is reading as decimal. Thus you need to convert the returned value (which is INT) to decimal before putting it into numericUpDown1.Value.

However, when using the value as part of a SQL query, you're converting it to string because you're using a query "string" to select values.

Member Avatar
ddanbe
Posting Expert
5,336 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 904 [?]
Skill Endorsements: 46 [?]
Featured
 
1
 

numericUpDown1.Value is ALWAYS of type decimal ,so an integer like 42 is auto converted, a double like 42.0 is not, in that case you can use the M suffix (42.0M), for strings use conversion methods.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

numericUpDown1.Value is ALWAYS of type decimal ,so an integer like 42 is auto converted, a double like 42.0 is not, in that case you can use the M suffix (42.0M), for strings use conversion methods.

<-- What he said! :twisted:

Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

So that is the solution? M suffix? I didn't used it before as i'm still a newbie.

How could I add it into my current code.

Member Avatar
ddanbe
Posting Expert
5,336 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 904 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

Use something like this perhaps:

string str = "3";
this.numericUpDown1.Value = Convert.ToDecimal(str);
Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

i still can't do it.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

Basically, keep it simple...

SqlCommand cmd = new SqlCommand("select * from student where student_no='" + numericupdown1.value.ToString() + "'",con)

and

numericUpDown1.Value = Convert.ToDecimal(dt.Rows[0].Columns["Bookshelf"]);

When performing SELECT from DB convert your decimal value to string. When pulling from DB to your control convert from whatever type you're pulling from the DB (you said INT) to decimal.

Member Avatar
AngelicOne
Posting Whiz
362 posts since Jul 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I got it! thanks! rep+

Question Answered as of 3 Years Ago by Lusiphur, ddanbe and __avd
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article