Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Following code shows error message while attempting to retrieve a table's records
on the base of selected valueMember property's value from combobox.

The error message is:- "input string was not in correct format"
comboBox2.DataSource = MainDataSet.Tables["Members"];
comboBox2.DisplayMember = "MemberName";
comboBox2.ValueMember = "MemID";  // int primary key of 'members' table 

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataRow[] bkrow;
    int n=Convert.ToInt32(ComBox1.SelectedValue.ToString());//<--This line shows error
    bkrow = MainDataSet.Tables[0].Select("MemID = '" + n + "'");   
    string QueryBookName = bkrow[0]["BookName"].ToString();
}


where memid is int primary key in Books table. How to solve this error?
Posted

This error is nothing to do with the database. This error is coming from the Convert.ToInt32 method. A better method to use would be int.Parse method. You will need to check whether ComBox1.SelectedValue.ToString() is actually a just a string containing a number or if it has invalid characters in it. See the MSDN coumentation on int.Parse for the valid input formats:

http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx[^]
 
Share this answer
 
v2
Comments
Nish Nishant 28-Apr-11 15:52pm    
My vote of 5!
Ed Nutting 28-Apr-11 17:47pm    
Thanks :)
Please pull your code fragment apart like this:

C#
comboBox2.DataSource = MainDataSet.Tables["Members"];
comboBox2.DisplayMember = "MemberName";
comboBox2.ValueMember = "MemID";  // int primary key of 'members' table

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataRow[] bkrow;
    String selectedValue = ComBox1.SelectedValue.ToString();
    if(!String.IsNullOrEmpty(selectedValue))
    {
        int n=Convert.ToInt32(selectedValue);//<--This line shows error
        bkrow = MainDataSet.Tables[0].Select("MemID = '" + n + "'");
        string QueryBookName = bkrow[0]["BookName"].ToString();
    }
}


Now place a breakpoint on the line with the if statement and inspect the variable slectedValue. The error is most likely caused by a value which cannot be converted to an integer.

Best regards,

-MRB
 
Share this answer
 
Comments
Nish Nishant 28-Apr-11 15:53pm    
My vote of 5!
sk saini 28-Apr-11 20:45pm    
But still its showing same error.
Manfred Rudolf Bihy 29-Apr-11 8:41am    
Did you debug the code like I told you to. Read my solution and do as you were told AND post the results of your findings if you can't make any sense of it.

Is that really asking to much of you?
Change this code
comboBox2.DataSource = MainDataSet.Tables["Members"];
to
comboBox2.DataSource = MainDataSet.Tables["Members"].DefaultView;
 
Share this answer
 
int n=Convert.ToInt32(ComBox1.SelectedValue.ToString());


As you said i think here from SelectedValue you get a value which is non-integer or null. This is the reason it is throwing that exception. Have you debugged what is the value?

Maybe instead you can try it like this:

int n=ComBox1.SelectedIndex;
 
Share this answer
 
v2
Comments
Ed Nutting 28-Apr-11 17:49pm    
.SelectedIndex would be nothing like int.Parse(SelectedValue) as the two almost definitely don't correspond :~
Rofl I am sorry :D But I may be crazy or you guys are completely ignoring his code.

Ok few notes to ponder in the above code block.
1. He binds the data source to "comboBox2"
2. He checks for selected index change in "ComBox1"
3. He gets the selected value from "ComBox1"

Since C# is CASE SENSITIVE, these are 3 different combo boxes. The problem MUST be here :)

@sk saini: Please, carefully read through your code again. Ensure you are referring to the CORRECT combo box and this is how you would retrieve the selected value from a combo box safely.

C#
// If the intended control is "comboBox2"
if (comboBox2.SelectedIndex != -1) {
     // Cast the selected value to the correct type
     int selectedValue = (int)comboBox2.SelectedValue;
}


Hope this helps :) Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900