Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net error: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

Tags:

c#

asp.net

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"]);
}
like image 881
user2721914 Avatar asked Dec 26 '22 21:12

user2721914


1 Answers

Change

this.txtHotel.Text = (dsRow["RoomsAvailable"]);

To

this.txtHotel.Text = (dsRow["RoomsAvailable"].ToString());
like image 173
Habib Avatar answered Dec 28 '22 10:12

Habib