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?

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

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)

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 :)

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.

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());

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.

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.

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.

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:

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.

Use something like this perhaps:

string str = "3";
this.numericUpDown1.Value = Convert.ToDecimal(str);

i still can't do it.

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.

I got it! thanks! rep+

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.