Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating a website project and I need to write data to textbox from database.
I used datareader. I am sure that it is connected to database, but it can not penetrate into while loop.
Could anyone help me?

Here is my code;
C#
SqlConnection conn = new SqlConnection();
SqlDataReader dr;

string TCNumber = Request.QueryString["TC"].ToString();
                    

SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'");

comm2.Connection = conn;

dr = comm2.ExecuteReader();

while (dr.Read())
{
       TCNo.Text = dr["TC"].ToString();
       Name.Text = dr["Name"].ToString();
       Surname.Text = dr["Surname"].ToString();
       BirthDate.Text = dr[3].ToString();
       Phone_Number.Text = dr[4].ToString();
}
dr.Close();
dr.Dispose();
conn.Close();
Posted
v2

You have not opened your connection.
so write
C#
comm2.Connection.Open();

after the line
C#
comm2.Connection = conn;

and before the line
C#
dr = comm2.ExecuteReader();

Hope it solves your problem.
Please let me know...
 
Share this answer
 
v2
Comments
sesenyg 1-Aug-12 4:02am    
No, unfortunatelly it is not, i did not add my connection lines there. The problem is not connection problem:(
Every time you go round your loop, you set the new value of each textbox, and discard any previous value. So if you have more than one row where TC is equal to TCNumber then only the last will be displayed - this is probably not what you want. I don't know exactly what you are trying to achieve, so I can't suggest a solution, other than "Try using += instead of ="

If you don't have any that match, nothing will be displayed at all. If not of your records are displaying, then either you have not records that match, or your querystring does not contain what you think it does.

And BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Hi Every thing is fine just open the connection as follows....

SqlConnection conn = new SqlConnection();
SqlDataReader dr;
 
string TCNumber = Request.QueryString["TC"].ToString();
                    
 
SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'");
 
comm2.Connection = conn;
  conn.Open();
dr = comm2.ExecuteReader();
 
while (dr.Read())
{
       TCNo.Text = dr["TC"].ToString();
       Name.Text = dr["Name"].ToString();
       Surname.Text = dr["Surname"].ToString();
       BirthDate.Text = dr[3].ToString();
       Phone_Number.Text = dr[4].ToString();
}
dr.Close();
dr.Dispose();
conn.Close();
 
Share this answer
 
v2
Comments
Himanshu Yadav 1-Aug-12 4:10am    
Hi Please pass connection string As per your data base.
SqlConnection conn = new SqlConnection("server=sam-pc;database=wipro;user id=sa;pwd=godfather")
Himanshu Yadav 1-Aug-12 4:13am    
is it working now?
Himanshu Yadav 1-Aug-12 4:26am    
For me it is working fine are sure there is data against that query
sesenyg 1-Aug-12 4:14am    
This is my whole code, i said the problem is not connection problem:(

SqlConnection conn = new SqlConnection();
SqlDataReader dr;
conn.ConnectionString = "server= ***; user = ***; pwd= ***; database = ***";
conn.Open();
string TCNumber = Request.QueryString["TC"].ToString();
SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from

MeasureRecords WHERE TC='"+TCNumber+"'");
SqlDataAdapter adap = new SqlDataAdapter(comm);

comm.Connection=conn;
DataSet ds = new DataSet();
adap.Fill(ds);

MeasureRecord.DataSource = ds;
MeasureRecord.DataBind();

SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'");
comm2.Connection = conn;
dr = comm2.ExecuteReader();

while (dr.Read())
{
TCNo.Text = dr["TC"].ToString();
Name.Text = dr["Name"].ToString();
Surname.Text = dr["Surname"].ToString();
BirthDate.Text = dr[3].ToString();
Phone_Number.Text = dr[4].ToString();
}
dr.Close();
dr.Dispose();
conn.Close();
sesenyg 1-Aug-12 4:18am    
And i can get data to my gridview 'MeasureRecord' so the problem is really not connection problem..
try this one.
C#
while (dr.Read())
{
       TCNo.Text = dr.GetString("TC");
       Name.Text = dr.GetString("Name");
       Surname.Text = dr.GetString("Surname");
       BirthDate.Text = dr.GetString(3];
       Phone_Number.Text = dr.GetString(4);
}
 
Share this answer
 
Comments
sesenyg 1-Aug-12 4:26am    
No, it did not work again:(
graciax8 1-Aug-12 4:40am    
are you sure your query string is getting a value. can you check the output of Request.QueryString["TC"].ToString(); first? thanks.
sesenyg 1-Aug-12 4:53am    
I have two sql command one of them work fine and i used my request.querystring value on my first sqlcommand. It returns the true value.
graciax8 1-Aug-12 5:10am    
declare a new connectionstring for your second query.
SqlConnection conn2 = new SqlConnection();

SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'");
comm2.Connection = conn2;

your code is kinda messy i suggest you clean it. hope this finally solves your problem. Goodluck!
sesenyg 1-Aug-12 6:17am    
thanks for your attention.
Change this line
SQL
SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC="+Convert.ToInt32(TCNumber)+"");


Your query is returning 0 row. So this is correct behavior.
Add breakpoint at while loop & check flow of execution .
When execution cone to breakpoint right click on dr -> quick watch and paste dr["TC"].ToString().
See the output you will get idea.
 
Share this answer
 
v4

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