Firstof all you need to execute your command object. You can achieve that with Execute method. A full example follows:
public DateTime GetServerDate()
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(@"SELECT GETDATE()", cn))
{
cn.Open();
return (DateTime)cmd.ExecuteScalar();
}
}
}
In this example we are calling
ExecuteScalar
method. It will return only the first column of first row of the fetched data. More information about it you can find here:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx[
^]
SqlCommand object offers also other methods as
ExecuteReader()
and
ExecuteNonQuery()
.
Basically you will call ExecuteReader() method and then bind the result to your combo.
[DataObject(true)]
public class DataService
{
public static DataTable GetAllByDetail()
{
DataTable table = new DataTable();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select name from details", cn))
{
cn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
table.Load(reader);
}
}
}
return table;
}
}
Now you can call this method, and bind the data to your combo.
private DataTable dt = DataService.Retrieve();
ComboBox1.DataSource = dt;
ComboBox1.DisplayMember = "Name";
ComboBox1.ValueMember = "Name";
I hope this answers all of your doubts.
Cheers