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
Pradip PandeyPosted Dec 25, 2012, 1:30 AM
cmbTest.DataSource = dsTest.Tables[0];
cmbTest.DataSource.DisplayMember="PKInvoiceID"
cmbTest.DataSource.ValueMember="PKInvoiceID"
cmbTest.DataBind();
Hope it will help.
varunPosted Dec 22, 2012, 1:10 AM
ALTER PROCEDURE GetTopInvoiceId AS
BEGIN
SELECT TOP 15 PKInvoiceId FROM Invoice_Master ORDER BY ID DESC
END
can you help pls.?
Pradip PandeyPosted Dec 21, 2012, 2:32 AM
Set 2 properties of combo box as
cmbTest.DataSource.DisplayMember="InvoiceName"
cmbTest.DataSource.ValueMember="InvoiceID"
Hope it will help.
varunPosted Dec 21, 2012, 1:42 AM
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];
Ish BandhuPosted Dec 21, 2012, 1:23 AM
cmbTest.DataSource = dsTest.Tables[0];
cmbTest.DataBind();
con.Close();