Link to home
Start Free TrialLog in
Avatar of JaimeJegonia
JaimeJegoniaFlag for United States of America

asked on

Why DataBind() is not Working but Read() do?

Dear Experts,

I just want to understand or to be enlightened why Databind() is not working in this procedure?

While using  this instead of DataBind():
        while ( DR.Read() )
            {
         DropDown.Items.Add( System.Convert.ToString( DR[ "Detail" ] ) );             
                                }

It works.


Thanks,


Jimi J.

********************* CODES *************************

// Create Instance of Connection and Command Object
                              OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["DatabaseCRTP1"]);

                              OleDbCommand myCommand = new OleDbCommand("QSRSBals", myConnection);

                              // Mark the Command as a SPROC
                              myCommand.CommandType = CommandType.StoredProcedure;

                              // Execute the command
                              DropDown.Items.Clear();
                              myConnection.Open();
                              
                              DropDown.DataTextField = "Detail";
                              DropDown.DataValueField = "SRSDetID";
                        
                              OleDbDataReader DR = myCommand.ExecuteReader( CommandBehavior.CloseConnection );
                              DropDown.DataSource = DR;
                               
                                        DropDown.DataBind();

                              //while ( DR.Read() )
                              //{
                              //      DropDown.Items.Add( System.Convert.ToString( DR[ "Detail" ] ) );
                        
                              //}

                              DropDown.Items.Insert(0,"-Pick-");
                              DropDown.SelectedIndex = 0;
                              
                              DR.Close();
                              myConnection.Close();




SOLUTION
Avatar of igor_alpha
igor_alpha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can assign a control datasource to a datareader as well
You are not making use iof DataValueField  and DatatextField properties

********************* CODES *************************

// Create Instance of Connection and Command Object
                         OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["DatabaseCRTP1"]);

                         OleDbCommand myCommand = new OleDbCommand("QSRSBals", myConnection);

                         // Mark the Command as a SPROC
                         myCommand.CommandType = CommandType.StoredProcedure;

                         // Execute the command
                         DropDown.Items.Clear();
                         myConnection.Open();
                         
                         DropDown.DataTextField = "Detail";
                         DropDown.DataValueField = "SRSDetID";
                   
                         OleDbDataReader DR = myCommand.ExecuteReader( CommandBehavior.CloseConnection );
                         DropDown.DataSource = DR;
          DropDown .DataTextField = "Detail";
          DropDown.DataValueField = "ENTER_KEY_VALUE_HERE";
                           
                                        DropDown.DataBind();

                         //while ( DR.Read() )
                         //{
                         //     DropDown.Items.Add( System.Convert.ToString( DR[ "Detail" ] ) );
                   
                         //}

                         DropDown.Items.Insert(0,"-Pick-");
                         DropDown.SelectedIndex = 0;
                         
                         DR.Close();
                         myConnection.Close();
SOLUTION
Avatar of Anandhi K
Anandhi K
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JaimeJegonia

ASKER

Desp,

I think both DataValueField and DataTextField are assigned with appropriate values as follows:
                         DropDown.DataTextField = "Detail";
                         DropDown.DataValueField = "SRSDetID";

Jimi J
igor_alpha

I tried your revised code. The same error (Page can't be displayed or stack oveflow...)

BTW, I'm executing this procedure to override the field control initialization.

Jimi J.
 

Actually, i just sticked to datareader and sorted with other alternative to come up with a result i desired.
Somehow, they gave me also ideas...

Thanks guys.