Inderjeet Kaur

Inderjeet Kaur

  • NA
  • 24
  • 3.2k

Display value from database in textbox using c# oop

Apr 9 2021 5:48 AM
class Balance : DbConnection
{
public string bal { set; get; }
public bool Load_Balance()
{
connectDb.Open();
SqlDataReader rd;
bool check = false;
using (var cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * Student WHERE Balance=@bal";
cmd.CommandType = CommandType.Text;
cmd.Connection = connectDb;
cmd.Parameters.Add("@bal", SqlDbType.VarChar).Value = bal;
rd = cmd.ExecuteReader();
while (rd.Read())
{
check = true;
}
connectDb.Close();
}
return check;
}
 
 
 
rd shows error bcz it doesn't exist here.
How can I fix it?
I have a connection class(DbConnection) and wanted to display value from database in another form. 
 
public void verify_bal()
{
Balance Bal = new Balance();
bool verify = Bal.Load_Balance();
if (verify == true)
{
txtBucks.Text = rd.GetValue(5).ToString;
}
else
{
MessageBox.Show("No!!");
}
}
 
 
 

Answers (1)