Hi,
I am Trying to Bind the data from data from database to combobox using a stored procedure. I am getting an Error saying:
InvalidOperationException
Fill: SelectCommand.Connection property has not been initialized.
Here is my code:
DataTable dtTest = new DataTable();
SqlConnection con=new SqlConnection(Helper.ConnectionString);
con.Open();
SqlCommand cmd=new SqlCommand();
cmd.CommandText="GetTopInvoiceId";
cmd.CommandType=CommandType.StoredProcedure;
SqlDataAdapter sdaTest = new SqlDataAdapter(cmd);
DataSet dsTest=new DataSet();
sdaTest.Fill(dsTest);
cmbTest.DataSource = dsTest.Tables[0];
con.Close();
Any suggestions Pls...
Loading

Ish BandhuRank #1,415 · 1.2K reputationPosted Dec 20, 2012, 8:23 PM EST
cmbTest.DataSource = dsTest.Tables[0];
cmbTest.DataBind();
con.Close();
varun9 reputationPosted Dec 20, 2012, 8:42 PM EST
I Made Few Changes to the code...
But The ouput is like this:
DataTable dtTest = new DataTable();
SqlConnection con=new SqlConnection(Helper.ConnectionString);
SqlCommand cmd=new SqlCommand();
cmd.CommandText="GetTopInvoiceId";
cmd.CommandType=CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataAdapter sdaTest = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
DataSet dsTest=new DataSet();
sdaTest.Fill(dsTest);
cmbTest.DataSource = dsTest.Tables[0];
Pradip PandeyRank #452 · 5.6K reputationPosted Dec 20, 2012, 9:32 PM EST
Set 2 properties of combo box as
cmbTest.DataSource.DisplayMember="InvoiceName"
cmbTest.DataSource.ValueMember="InvoiceID"
Hope it will help.
varun9 reputationPosted Dec 21, 2012, 8:10 PM EST
ALTER PROCEDURE GetTopInvoiceId AS
BEGIN
SELECT TOP 15 PKInvoiceId FROM Invoice_Master ORDER BY ID DESC
END
can you help pls.?
Pradip PandeyRank #452 · 5.6K reputationPosted Dec 24, 2012, 8:30 PM EST
cmbTest.DataSource = dsTest.Tables[0];
cmbTest.DataSource.DisplayMember="PKInvoiceID"
cmbTest.DataSource.ValueMember="PKInvoiceID"
cmbTest.DataBind();
Hope it will help.