-2

Im writing C# program connecting with mysql. In my sql i have two tables one called car and other one called model .In my car table i have a column named modele_vehiclue with datatype int and also it is a foreign key.Second table called model which is made of two columns modele_vehiclue datatype int and model_name varchar the problem starts when i run my program and enter data in my boxes it says you can convert a int to var char values .This is the code I'm using in my c# . if you could help me with it you are making my day .

cmd.Parameters.Add(new SqlParameter("@modele ", modele_vehiclue.Text));
8
  • tnx nico for taking ur time to answer my question .i saw this post but this is not what im looking for Commented Mar 16, 2017 at 1:57
  • Its exactly what your looking for. Make sure you read the whole set of answers and not just look at the highest rated one. stackoverflow.com/questions/3115678/… (further down) shows that you can use int.Parse(string) and if your not sure if the result will be a valid int then use int.TryParse(string) Commented Mar 16, 2017 at 1:58
  • nop bro haha tell me how this is exactly samething Commented Mar 16, 2017 at 2:01
  • cmd.Parameters.Add(new SqlParameter("@modele ", int.Parse(modele_vehiclue.Text))); Commented Mar 16, 2017 at 2:02
  • it says "input string was not in a correct format" Commented Mar 16, 2017 at 2:06

2 Answers 2

1

I think Parameters.Add is more suitable in this case to specify the DataType of the value. for that you can use the SqlDbType Enumeration, and you can use int.Parse() or int.TryParse() for parsing as well. So You can try something like this:

int modele = 0;
if(int.TryParse(modele_vehiclue.Text, out modele)
{
   cmd.Parameters.Add("@modele", SqlDbType.Int).Value = modele;
}
else
{
    // display message that invalid input
}
Sign up to request clarification or add additional context in comments.

11 Comments

i tired this bro it says "input string was not in a correct format"
So the issue is with the input that you are giving, which is not a valid integer and that's why int.parse failed to convert them. You can trust int.TryParse in this case. see the updated answer
thank you man .last questions what if i want do the same process other way around like convert int to string
@afgboy: just use .ToString() to convert an integer to a string
int modele = 0; if(,ToString(modele_vehiclue.Text, out modele) { cmd.Parameters.Add("@modele", SqlDbType.Int).Value = modele; } else { // display message that invalid input }
|
0

In the interst to answer this correctly.

Converting a string to an int

This is done really easily with one of the various built in methods such as.

int.Parse(string s) : This will attempt to convert the value passed as s to an int (Int32). If the input string s is not a valid int the exception FormatException will be thrown

There are multiple overrides as listed here int.Parse Method

Now the key here the string must be a valid int representation anything else will throw the FormatException

So this can be used as

cmd.Parameters.Add(new SqlParameter("@modele ", int.Parse(modele_vehiclue.Text)));

How to safely validate the string

Now as we have just stated the string must be a valid int representation for this to succeed. Now if you are not 100% sure that the value of modele_vehiclue.Text property is a valid string representation of an int then you should validate it first.

Luckily this is really easy. There is a built in method called int.TryParse(string s, out int result method which returns a Boolean result (not an int).

So this could be used as.

int modele = 0;
//int.TryParse returns a true\false on operation success. The value of modele will be the converted int or zero if the operation failed.
if(int.TryParse(modele_vehiclue.Text, out modele) == true)
{
     //conversion success
     cmd.Parameters.Add(new SqlParameter("@modele ", modele);
}
else
{
     //handle failed conversion
     throw new Exception("modele_vehiclue.Text must be an int");
}

5 Comments

It is not necessary to check with ` == true` since int.TryParse returning a boolean
@un-lucky i understand that but given the comments on the post figured best to include that.
@Nico thank you man .last questions what if i want do the same process other way around like convert int to string
so just need to replace int.TryParse in the if with ToString >
Converting an int to string is very easy int.ToString(). say int modele = 0. Then you call string y = modele.ToString()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.