I want to retrieve data from a table in sql server called hotel using select WHERE statement and I get the above error. Can anyone help?
SqlConnection cnn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT RoomsAvailable FROM Hotel WHERE HotelName = '" +
this.DropDownList1.Text + "'";
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Hotel");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow dsRow = null;
foreach (DataRow dsRow_loopVariable in ds.Tables["Hotel"].Rows)
{
dsRow = dsRow_loopVariable;
//This line is where the error comes in.
this.txtHotel.Text = (dsRow["RoomsAvailable"]);
}
Change
this.txtHotel.Text = (dsRow["RoomsAvailable"]);
To
this.txtHotel.Text = (dsRow["RoomsAvailable"].ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With