1

I am working with Microsoft SQL Server Management Studio, and C# .net

My code fetches string data from the webBrowser, stores it in a textbox, compares it with values in my database, and gets the corresponding int ID to that string. Now, this int ID is displayed in a textbox (optional), and is then stored in another table in my database.

My code is as follows:

string p = dComm.executeScalar("select StateID from StateMaster where StateCode='" + txtState.Text + "'");
textBox1.Text = p;
Convert.ToInt32(p);

This fetches the StateID, and displays it in a textbox. I then open another table from the database, and put the value as shown below.

dTbl = dComm.openDataTable("CompanyMaster", "select * from CompanyMaster where 1=0 ");
DataRow dRow;
dRow = dTbl.NewRow();
dRow["StateID"] = p;

Now, this code works perfectly fine when i run the code on the HTML file of the page, but if I try to run it directly from the web browser, it gives an error

Input String was not in a correct format

The error is shown at the line:

Convert.ToInt32(p);

Am I missing something? Can anyone help me with this?

12
  • This would happen if the string isn't actually a number. Commented Jul 24, 2013 at 15:24
  • What value is coming back from StateID? It must not be a valid integral format. I'd just debug it and see what p is. Commented Jul 24, 2013 at 15:24
  • It is a number. It fetches value '36' when i run in through HTML code. Commented Jul 24, 2013 at 15:25
  • 1
    Could be that you're getting into an initial state where the value of the text box is an empty string and trying to process that. What do you mean by running through HTML code vs. directly from the browser ? Commented Jul 24, 2013 at 15:26
  • 1
    By the way, metadata like State ID is not usually displayed since it's meaningless to the user. It should be in a hidden field at worst but more likely a property of your view or session. Don't hack HTML with things like that. This isn't 1995. Commented Jul 24, 2013 at 15:33

2 Answers 2

1

The problem you are having:

Convert.To is relatively hard work, and shouldn't (in my eyes) be used when the input can come from the end user. When bad data is provided to the Convert.To it gives exceptions that cannot be handled very well.

For example:

 textbox1.Text = "test";
 Convert.ToInt32(textbox1.text);

It will crash and burn.

I would advise you get into the routine of using TryParse, which handles bad values much better.

Example:

int test = 0;
int.TryParse(textbox1.text, out test)
{
      //do validation with if statements
}

This would cancel out any bad data, and also ensure that the end user would only get user friendly message boxes that you have wrote yourself. Meaning you can tell them where they have put in a bad value so that they can correct it without the program crashing and burning.

Sign up to request clarification or add additional context in comments.

1 Comment

Similar answer to mine, but it's well written and helpful, so +1 for you!
0

Try using Int32.Parse(p) instead. You can also use the TryParse if you don't want an exception on a failed parse, and instead you would get a bool that shows whether it was successful or not.

So from your example, it would be like this...

string p = dComm.executeScalar("select StateID from StateMaster where StateCode='" + txtState.Text + "'");
textBox1.Text = p;
int pInt = Int32.Parse(p);

Or if you want to use TryParse, it would be like this...

string p = dComm.executeScalar("select StateID from StateMaster where StateCode='" + txtState.Text + "'");
textBox1.Text = p;
int pInt;
if(!Int32.TryParse(p, out pInt))
{
  //Parsing failed, do something about it
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.