私はラベルにデータリーダーから列を読み取るためにしようとしている(C#のWinフォーム)は次のよう 私のコードは次のとおりです。C#のWinFormsの引数1:「INT」に「文字列」から変換することはできません
SqlCommand command1 = new SqlCommand("select plant_name,plant_id from plant order by plant_id ", connection);
try
{
connection.Open();
SqlDataReader dr = command1.ExecuteReader();
while (dr.Read())
{
string plantlable = dr.GetInt32("plant_id").ToString();
labelplantid.Text = plantlable.ToString();
comboBoxplant.Items.Add(dr["plant_name"]);
}
dr.Close();
dr.Dispose();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
赤い下線plant_idで
string plantlable = dr.GetInt32("plant_id").ToString();
次の行に:
私はエラー「『int型』から 『文字列』から変換することはできません引数1」を取得します。
私は間違っていますか? 私はそれを把握していないようです。 plant_idは列型Intです。 データベース用にSQL Server 2008を使用する。
ヒントありがとうございます。
数字以外の文字列から整数値を取得しようとしましたが、意味がありません。代わりに 'dr [" plant_id "]'を使うべきです。 'GetInt32'メソッド情報についてはhttps://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint32(v=vs.110).aspxを参照してください。 –
ありがとうございました..これはうまくいきました.... labelplantid.Text = dr ["plant_id"]。ToString(); – Harry