开发者

Cannot implicitly convert type 'int' to 'string'

My code:

public void FillMaxBankCode()
{
    try
    {
        DataSet ds = new DataSet();
        ds = bol.SelectMaxBankCode();
        stri开发者_如何学运维ng j = ds.Tables[0].Rows[0].ToString();
        txtbankid.Text = int.Parse(j); //ERROR HERE
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}



  public DataSet SelectMaxBankCode()
    {
        try
        {
            **Squery = "EXEC SelectMaxBankCode";**
            return dal.DBread(Squery);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I am new to C# and the above code show error.. can anyone help?


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


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);
            }
        }


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜