i have a method that will populate a datatable with the data from a sql table which in turn would be used to populate a gridview. Now in the sql table i have a field called “hotel” which contains the “ID” of a hotel which relates to another table called “hotels” with hotel id and name.
Now in my grid view i want to display the hotel name instead of the hotel id. How can i do it.
public static DataTable GetRequests(string empid)
{
DataTable dt = new DataTable();
string strConnection = ConfigurationManager.AppSettings["connStr"];
using (SqlConnection connection = new SqlConnection(strConnection))
{
connection.Open();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter sAdap = new SqlDataAdapter();
sqlcmd.Connection = connection;
sqlcmd.CommandType = System.Data.CommandType.Text;
sqlcmd.CommandText = "Select request_date,hotel,dining_date,status from requests Where emp_id='" + empid + "'";
sAdap.SelectCommand = sqlcmd;
sAdap.Fill(dt);
}
return dt;
}
This is the method that retrieves the records. The hotel field contains the ID for which i want the name.
Change to your sql query and thing will work out.
Let us say ‘Hotel’ which contain hotel id and in the another table ‘HotelDetails which contain the hotel id and hotel name.
Table structure of Hotel
HotelId int
Table structure of HotelDetails
HotelId int,
HotelName varchar(10)
Now you query should be
In the GridView, your DataFild should be HotelName in order to display Hotel Name
For instance
If you find it useful, please mark it as your answer else let me know….