Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void BindReader(string storedproc,string[] param)
       {
           clear();
           reader = ppt.ParamReader(storedproc,param ); // Here searching a Branch Name and binding values in matching text box
           while (reader.Read())
           {
               lbl_ID.Text = reader["BRANCH_ID"].ToString();
               txt_Address.Text = reader["BRANCH_ADDRESS"].ToString();
               txt_Branch_Phone.Text = reader["BRANCH_PHONE"].ToString();
               txt_CST_Number.Text = reader["CST_NUMBER"].ToString();
               txt_tin_number.Text = reader["TIN_NUMBER"].ToString();
               cmb_Branch_Name.Text = reader["BRANCH_NAME"].ToString();
           }
           reader.Close();
       }

C#
public SqlDataReader ParamReader(string sp, string[] pp)
{
    if (_connection.State == ConnectionState.Open)
    {
        _connection.Close();
    }
    _connection.ConnectionString = setConnection();
    try
    {
        _connection.Open();
        cmd = parameters(sp, pp, _connection);// Calls Parameters Method to load the Parameters in the command
        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }
    return reader;
}


<code>
This code is working at very first time. if I call this method again it returns error like invalid attempt when data reader is closed.
what is the reason? what should i do for this to over come this?
Posted

You are closing your connection here.
if (_connection.State == ConnectionState.Open)    {        _connection.Close();    }


That is why you get this error when you run your code the next time.
 
Share this answer
 
Hi ,

I think you have declare reader global and close in BindReader method.
And try open reader in cmd.ExecuteReader(CommandBehavior.CloseConnection).

So first instance with it new instance before this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900