简体   繁体   English

输入的字符串格式不正确

[英]Input string was not in a correct format

Anyone Please help.... 任何人请帮忙。

Error - 错误-

Input string was not in a correct format. 输入的字符串格式不正确。

int comboxchoice = Int32.Parse(comboBox1.SelectedValue.ToString());

i am using below code 我正在使用下面的代码

private void Form2_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
            conn.Open();
            SqlCommand command = new SqlCommand("select * from table1", conn);
            SqlDataAdapter adp = new SqlDataAdapter(command);
            tbl = new DataTable();
            adp.Fill(tbl);
            comboBox1.DataSource = tbl;
            comboBox1.ValueMember = tbl.Columns[0].ColumnName;
            comboBox1.DisplayMember = tbl.Columns[0].ColumnName;
      }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
            conn.Open();
            SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
            int comboxchoice = Int32.Parse(comboBox1.SelectedValue.ToString());
           command.Parameters.AddWithValue("@id",comboxchoice);
             SqlDataAdapter adp = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adp.Fill(ds, "table1");
            textBox1.Text = ds.Tables["table1"].Rows[0]["name"].ToString();
           }
    }

I couldn't see which method you do need but the error you are receiving is because SelectedValue is returning a DataView (according to your comment). 我看不到您需要哪种方法,但是收到的错误是因为SelectedValue返回了DataView (根据您的评论)。

According to the Msdn documentation for DataView , under ToString, 根据DataViewMsdn文档 ,在ToString下,

Returns a String containing the name of the Component 返回一个字符串,其中包含组件的名称

As a start you can try casting 首先,您可以尝试投射

(DataView)comboBox.SelectedValue

But this should include extra error handling to ensure that SelectedValue is a valid DataView. 但这应包括额外的错误处理,以确保SelectedValue是有效的DataView。


As a side note, you ccould also avoid this with better number parsing 附带说明,您还可以通过更好的数字解析来避免这种情况

int comboChoice;
if(int.TryParse((DataView)comboBox.SelectedValue, out comboChoice))
   //Do something
else
    //Number invalid, do something else

Because comboBox1.SelectedValue is not returning a correct integer value. 因为comboBox1.SelectedValue没有返回正确的整数值。

You success to change comboBox1.SelectedValue to string, but the string cannot convert to corresponding integer because it is not integer . 您成功将comboBox1.SelectedValue更改为string,但是该字符串不能转换为相应的整数,因为它不是integer

For example, the following statement triggers same error 例如,以下语句触发相同的错误

        //non-integer
        string omg = "2313asd";
        int convertedINT = Int32.Parse(omg.ToString());

Get idea from @Vignesh Kumar, empty string is not recognize as integer format also. 从@Vignesh Kumar了解想法, 空字符串也不能识别为整数格式。

        //empty string
        string omg = "";
        int convertedINT = Int32.Parse(omg.ToString());

If you confirm only empty string triggers your error , then only check for the empty string with string.IsNullOrEmpty() 如果确认只有空字符串会触发错误,则仅使用string.IsNullOrEmpty()检查空字符串。

if you want to eliminate the error totally , then confirm your comboBox1.SelectedValue is correct integer format with Int32.TryParse() . 如果要完全消除错误,请使用Int32.TryParse()确认您的comboBox1.SelectedValue是正确的整数格式。

        int comboxchoice;
        if (Int32.TryParse(comboBox1.SelectedValue.ToString(), out comboxchoice))
        {
            proceed by using comboxchoice as converted int value
        }
        else
        {
            //not integer value.....
        }

or Maybe you can display the string can check if it is integer value as you imagine 或也许您可以显示字符串,可以按照您的想象检查它是否为整数

        MessageBox.Show(comboBox1.SelectedValue.ToString());

Can't you get rid of the "ToString()". 您不能摆脱“ ToString()”。 I believe the "SelectedValue" itself is a string value. 我相信“ SelectedValue”本身就是一个字符串值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM