I have a SQL Server database file MsUser.mdf
with a table MsAccount
that has 5 columns:
userID accountID accountName accountBalance imageLocation
I need to find the accountBalance
where accountID = combobox
that being selected, and show it in labelBalance._text
. AccountBalance
is decimal
, accountID
is varchar(10)
.
I wrote the code at comboBox event selected index. Thanks for helping.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = comboBox2.SelectedItem.ToString();//typ0000001-cake
int position = selected.IndexOf("-");
string accountID = selected.Substring(0,position);//typ0000001
SqlDataAdapter sdaUserID = new SqlDataAdapter("Select Count(accountBalance),accountBalance From MsAccount where accountID='" +accountID+"'", cn);
DataTable dt1 = new DataTable();
sdaUserID.Fill(dt1);
lblBalance.text = dt1.Rows[0][1].ToString();
}