Dataadapter and where clause.

S

Shum

Hi!
I want to restrict the selected records with a WHERE clause in
dataadapter... but when i do that no rows are retrieved... Any
alternatives?? Any suggestions? (using sql server 2005 and c#)
..
..
..
..
while (drr.Read())
{
hosp=System.Convert.ToString(drr["HospitalName"]);


SqlDataAdapter da = new SqlDataAdapter("SELECT TotalPatients
from Patients
WHERE HospitalName='hosp' ",con1);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds,0,0,"Patients");

int rowcount=System.Convert.ToInt16(da.Fill(ds,0,0,"Patients"));
for (int i; i<=rowcount; i++)
{
int totpat=System.Convert.ToInt32(ds.Tables["Patients"].Rows
["TotalPatients"]);
MessageBox.Show(Convert.ToString(totpat));
}

}
..
..
..
 
M

Miha Markic

Why are you using SqlCommandBuilder? If you create dataadapter manually you
don't need command builder...
 
G

Guest

Shum,

Your sql statement is looking for hospitals named "hosp" and you probably
don't have any with that name.

The name of the hospital you are looking for is in a variable named hosp and
you need to supply the contents of that variable to the select statement, not
the name of the variable.

Two ways to do that:

1. Concatenate the variable value with the text of the sql statement. This
works but can lead to sql injection attacks.

2. Use parameters to supply the value. This is the preferred technique.

Kerry Moorman
 
S

Shum

I get it... Thanks!


Shum,

Your sql statement is looking for hospitals named "hosp" and you probably
don't have any with that name.

The name of the hospital you are looking for is in a variable named hosp and
you need to supply the contents of that variable to the select statement, not
the name of the variable.

Two ways to do that:

1. Concatenate the variable value with the text of the sql statement. This
works but can lead to sql injection attacks.

2. Use parameters to supply the value. This is the preferred technique.

Kerry Moorman



Shum said:
Hi!
I want to restrict the selected records with a WHERE clause in
dataadapter... but when i do that no rows are retrieved... Any
alternatives?? Any suggestions? (using sql server 2005 and c#)
..
..
..
..
while (drr.Read())
{
hosp=System.Convert.ToString(drr["HospitalName"]);
SqlDataAdapter da = new SqlDataAdapter("SELECT TotalPatients
from Patients
WHERE HospitalName='hosp' ",con1);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds,0,0,"Patients");
int rowcount=System.Convert.ToInt16(da.Fill(ds,0,0,"Patients"));
for (int i; i<=rowcount; i++)
{
int totpat=System.Convert.ToInt32(ds.Tables["Patients"].Rows
["TotalPatients"]);
MessageBox.Show(Convert.ToString(totpat));
}

}
..
..
..- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top