Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,Everyone

I am creating a web based project and I need to write data to textbox from database.
i have made a function that return sqldatareader but how can i write this data that came from sqldatareader into text boxes. can anybody show me code for this...


What I have tried:

C#
public SqlDataReader GetApproveDemand(string query)
{
    SqlDataReader reader = null;
        DBConnection().Open();
        SqlCommand cmd = new SqlCommand(query, DBConnection());
        reader = cmd.ExecuteReader();
        return reader;
}



MY code in aspx.cs file is as follow


C#
db db = new db();
       string query = "Select Item_ID,Emp_ID,Demand_Date,Demand_Quantity From Demands WHERE Demand_ID = '" + txt_Search_Demand_ID.Text + "'";
       SqlDataReader reader = db.GetApproveDemand(query);

       while (db.GetApproveDemand(query).Read())
       {
           txt_Approve_Item_ID.Text = (reader["Item_ID"].ToString());
           txt_Approve_Employee_ID.Text = (reader["Emp_ID"].ToString());
           txt_Approve_Date.Text = (reader["Demand_Date"].ToString());
           txt_Approve_Quantity.Text = (reader["Demand_Quantity"].ToString());
       }
Posted
Updated 19-Dec-16 0:29am
Comments
hari19113 16-Dec-16 3:49am    
What is the O/P after executing above code, does it shows some error? If yes please mention that.
Muhammd Aamir 16-Dec-16 6:16am    
thank you very much hari19113 for reply.... this code always give me the error "sqldatareader required an open and available connection the current connection state is closed" i have tried sqldatareader a lot but did not work for me .... after that i does this with datatable.... if you have solution with sqldatareader then please show me ......
[no name] 16-Dec-16 8:29am    
The error message is kind of need to know information for someone that is trying to help you. And if you read it, it is very clear. You need to learn how to debug your code and find out why your connection isn't open.
j snooze 16-Dec-16 17:29pm    
Agree with NotPolitically correct. If you step through the code after the ExecuteReader() call and hover your mouse over the reader variable...is it closed? If so there is probably something wrong with your SQL statement, or its not returning any data.
Muhammd Aamir 19-Dec-16 4:27am    
NotPolitcallyCorrect And j snooze thank you for sharing your knowledge. i solved my this the problem was in SQL connection in my static function that was return sqlconnection i haven't opened connection there.... thankx again

1 solution

As you didn't post DBConnection(), I'm assuming you are creating SQLConnection object inside it.
I've created a working sample.

C#
SqlConnection con=null;
Public SqlConnection DBConnection()
{
   string constring=<Your Connection String>;
   if (con==null)
   {
      con=new SqlConnection(constring);
   }
   return con;
}
Public SqlDataReader GetName(string Query)
{
   SqlDataReader dr=null;
   DBConnection().Open();
   SqlCommand cmd=new SqlCommand(Query,DBConnection());
   dr=cmd.ExecuteReader();
   return dr;
}
Public void DisplayName()
{
   DB db=new DB();
   string Query="select name from demo where id = 1";
   SqlDataReader dr=db.GetName(Query);
   while(dr.read())
   {
      txtName.Text=dr["name"].toString();
   }
}


Hope this helps you.
 
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