Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Clifford Chapman   on Aug 27 In Asp.net Category.

  
Question Answered By: Devlan Jones   on Aug 27

Don't close  the DataReader until you have
bound the controls:


private void BindListBox(DropDownList listControl, string Sql, string dataText,
string dataValue, string listText)

OleDbDataReader dr;

dr = DBUtility.GetDataReader(Sql);

if(dr != null)

{

if(dr.FieldCount > 0)

{

listControl.DataSource = dr;

listControl.DataTextField = dataText;

listControl.DataValueField = dataValue;

listControl.DataBind();

listControl.Items.Insert(0, listText);

listControl.SelectedIndex = 0;

}

}

dr.Close();

}

To get DataReader:

public static OleDbDataReader GetDataReader(string SQLString)

{

OleDbConnection cn = new OleDbConnection(DBConnString);

OleDbDataReader dr;

try

{

cn.Open();

OleDbCommand cmd = cn.CreateCommand();

cmd.CommandText = SQLString;

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}

catch (Exception e)

{

throw e;

}

return dr;

}

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on dataset vs. datareader Or get search suggestion and latest updates.


Tagged: