Cannot implicitly convert type 'int' to 'string'

前端 未结 4 1176
情话喂你
情话喂你 2021-01-28 01:26

My code:

public void FillMaxBankCode()
{
    try
    {
        DataSet ds = new DataSet();
        ds = bol.SelectMaxBankCode();
        string j = ds.Tables[0].         


        
相关标签:
4条回答
  • 2021-01-28 01:41

    int.Parse(..) is for converting a string containing digits into an integer. You are trying to perform the function int.Parse(..) on a string but then assign it to another string. That won't work because int.Parse(..) returns and integer. Read here about Int32.Parse (String) method.

    0 讨论(0)
  • 2021-01-28 01:44

    txtbankid.Text property type is a string. Don't use int.parse. There is no need to. Just do: txtbankid.Text = j;

    0 讨论(0)
  • 2021-01-28 01:48
    public void FillMaxBankCode()
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds = bol.SelectMaxBankCode();
                    string j = ds.Tables[0].Rows[0].ToString();
                    txtbankid.Text = j;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    0 讨论(0)
  • 2021-01-28 01:50

    If you want to ensure that the value is an integer before assigning it I suggest you use TryParse

    // check if the table and row exists
    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
    {
        string j = ds.Tables[0].Rows[0].ToString();
        int value = 0;
        if (int.TryParse(j, out value))
            txtbankid.Text = value.ToString();
    }
    
    0 讨论(0)
提交回复
热议问题