public partial class Form : System.Web.UI.Page
{
    private void connecttodb()
    {
        OracleConnection conn = new OracleConnection();
        conn.ConnectionString = "User Id=WLL; Password=wll; Data Source=WLL;";
     }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            String CommandText = "Select DepartmentID,Department from its_department;";
            OracleDataReader odr = GetDr(CommandText, ConnectionString);
            DropDownList1.DataSource = odr;
            DropDownList1.DataTextField ="Department";
            DropDownList1.DataValueField ="DepartmentID";
            DropDownList1.DataBind();
        }
   private OracleDataReader GetDr(String sqltext, String ConnectionString)
            {
              OracleDataReader dr;
              OracleConnection oracle_conn = new OracleConnection(ConnectionString);
              OracleCommand Oracle_cmd = new OracleCommand( sqltext, conn );
               Oracle_cmd.Connection.open();
              dr = Oracle_cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
              return dr;
            }
   
}

Recommended Answers

All 2 Replies

The only mistak is you are not passing connection string to your finction
i.e. GetDr(String sqltext, String ConnectionString). the only the command is passing at this function but connection string is not initialise that's why it gives error.
for recovery create one public string for connection string
string cons="User Id=WLL; Password=wll; Data Source=WLL;";

and then pass this string whenever you call function
GetDr(Commandtext,cons)
try it. if you got your answer mark thread as solved.

Only thing I'm going to say here is... when did DaniWeb become the equivalent of a compiler's built-in syntax and error checker?

If you have a specific problem with the code or it's not working in an expected way it would be appreciated if you would provide the error details for the community here to work with instead of plastering a chunk of code (without the code formatting braces) into the page with the title "check the code for errors".

commented: Agreed. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.