Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 beginners problem with simple sql statement

Author  Topic 

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-12 : 04:52:40
I have two problems:
1: My sql-statement doesn't return anything.
2. How do I change it into using parameters correctly?

Here's my whole button-click without parameters:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
ListBoxSearchCust.Items.Clear();
string Street = TextBoxStreet.Text;
SqlConnection conn = new SqlConnection(config);
conn.Open();

string sql = "SELECT ID, firstname, lastname FROM Customer WHERE street='" + Street + "'";

SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader dr = comm.ExecuteReader();

ListBoxSearchCust.Items.Add("Result, customer:");

while (dr.Read())
{
ListBoxSearchCust.Items.Add(dr[0] + " " + dr[2] + ", " + dr[1]); //Error - nothing is displayed
}
dr.Close();
conn.Close();
}


And here's where I try to use parameters:

string Street = TextBoxStreet.Text;
SqlConnection conn = new SqlConnection(config);
conn.Open();

string sql = "SELECT ID, firstname, lastname FROM Customer WHERE street=" + @Street;

SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add("@Street", SqlDbType.VarChar);
comm.Parameters["@Street"].Value = Convert.ToString(TextBoxStreet.Text);
SqlDataReader dr = comm.ExecuteReader(); //Error: "Incorrect syntax near '='".

Thanks in advance!

Sachin.Nand

2937 Posts

Posted - 2010-10-12 : 05:38:26
Are you sure there exists records in the DB for the given query?

PBUH

Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-12 : 06:16:30
Yes, I've checked several times :) Maybe the error lies somewhere else in the program, and not with the sql statement...
If so, that leaves question no2 still to be answered.
Go to Top of Page

kishore_pen
Starting Member

49 Posts

Posted - 2010-10-12 : 06:45:00
If there is no text in TextBoxStreet.Text then you didn't get any result. You can write try the following way:

if (TextBoxStreet.Text.Length > 0)
{
string sql = "SELECT ID, firstname, lastname FROM Customer WHERE street='" + Street + "'";

SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader dr = comm.ExecuteReader();

ListBoxSearchCust.Items.Add("Result, customer:");

while (dr.Read())
{
ListBoxSearchCust.Items.Add(dr[0] + " " + dr[2] + ", " + dr[1]); //Error - nothing is displayed
}
dr.Close();
conn.Close();
}
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-12 : 07:15:42
TextBoxStreet is not empty, thanks anyway.
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-12 : 07:46:37
I tried another thing just to see if it works; I made a button "Show all" that shows every record in this particular database table and it does:
protected void ButtonShowAll_Click(object sender, EventArgs e)
{
ListBoxSearchCust.Items.Clear();
SqlConnection conn = new SqlConnection(config);
conn.Open();
string sql = "SELECT * FROM Customer";
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader dr = comm.ExecuteReader();
ListBoxSearchCust.Items.Add("Result, customer:");
while (dr.Read())
{
ListBoxSearchCust.Items.Add(dr["ID"] + " " + dr["lastname"] + ", " + dr["firstname"]);
}
dr.Close();
conn.Close();
}

I still don't get whats wrong with my code, though...
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-12 : 08:01:49
What happens if you use adapter instead of a reader?

DataTable dt =new DataTable();
SqlDataAdapter da = New SqlDataAdapter(command)
da.Fill(dt)


& then bind it with the DataTable with the listbox.

PBUH

Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 06:34:29
I've tried using an adapter, and now it seems that the sql statement just doesn't find the record "street" even though it does exist.
This is what the code looks like now:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
ListBoxSearchCust.Items.Clear();
string Street = TextBoxStreet.Text;

if (TextBoxStreet.Text != null)
{
SqlConnection cn = new SqlConnection(config);
DataSet ds = new DataSet("ds");
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, firstname, lastname, street FROM Customer WHERE street='" + Street + "'", cn);
da.Fill(ds);
ListBoxSearchCust.DataSource = ds;
if (TextBoxStreet.Text != "street")
{
LabelNoMatch.Visible = true; //this error message is showing
}
else
{
ListBoxSearchCust.DataTextField = "ID" + " " + "lastname" + ", " + "firstname";
ListBoxSearchCust.DataBind();
}
}
else
{
LabelNoStreet.Visible = true;
}
}
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-14 : 06:48:59
What does ds.Tables[0].Rows.Count return ?

PBUH

Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-14 : 06:49:52
quote:
Originally posted by KarinElvira

I've tried using an adapter, and now it seems that the sql statement just doesn't find the record "street" even though it does exist.
This is what the code looks like now:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
ListBoxSearchCust.Items.Clear();
string Street = TextBoxStreet.Text; --You are assiging value to variable. Ex: The value can be King too.
if (TextBoxStreet.Text != null)
{
SqlConnection cn = new SqlConnection(config);
DataSet ds = new DataSet("ds");
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, firstname, lastname, street FROM Customer WHERE street='" + Street + "'", cn);
da.Fill(ds);
ListBoxSearchCust.DataSource = ds;
if (TextBoxStreet.Text != "street") --Here you are checking with Hardcoded value street and not King (as per example)
Secondly I don't see where you are chaging the value of textboxstreet.
{
LabelNoMatch.Visible = true; //this error message is showing
}
else
{
ListBoxSearchCust.DataTextField = "ID" + " " + "lastname" + ", " + "firstname";
ListBoxSearchCust.DataBind();
}
}
else
{
LabelNoStreet.Visible = true;
}
}



I am not good in .Net so if my understanding is wrong then apologies from my side.
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 08:16:54
pk_bohra: I think you misunderstand my code.

string Street = TextBoxStreet.Text; //Street is the string displayed in the textbox, whatever string it may be

if (TextBoxStreet.Text != "street") //Here I'm comparing what's in the textbox (string Street) to the record in database column street
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 08:31:40
quote:
Originally posted by Sachin.Nand

What does ds.Tables[0].Rows.Count return ?

PBUH


Is that something I should add to my code?
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-14 : 08:45:12
If it is a windows application just add MessageBox.Show(ds.Tables[0].Rows.Count)after u fill the dataset
or
if it is a web application add a label with id as lblcount to the page & set add this code after u fill the dataset lblcount.text=ds.Tables[0].Rows.Count.ToString()

PBUH

Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 09:22:49
quote:
Originally posted by Sachin.Nand

If it is a windows application just add MessageBox.Show(ds.Tables[0].Rows.Count)after u fill the dataset
or
if it is a web application add a label with id as lblcount to the page & set add this code after u fill the dataset lblcount.text=ds.Tables[0].Rows.Count.ToString()

PBUH



It shows 0 (zero), and when I put a breakpoint at the sql statement and debug, Street shows as empty ("").

SqlDataAdapter da = new SqlDataAdapter("SELECT ID, firstname, lastname, street FROM Customer WHERE street='" + Street + "'", cn);

Only it hasn't been empty any of the times I've clicked the button. Is there something wrong with this?:
string Street = TextBoxStreet.Text;
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-14 : 10:22:16
Try this,

protected void ButtonSearch_Click(object sender, EventArgs e)
{
ListBoxSearchCust.Items.Clear();
string Street = TextBoxStreet.Text;

if (TextBoxStreet.Text != null)
//Put breakpoint here
{
SqlConnection cn = new SqlConnection(config);
DataSet ds = new DataSet("ds");
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, firstname, lastname, street FROM Customer WHERE street='" + Street + "'", cn);
da.Fill(ds);//put breakpoint here check street value
ListBoxSearchCust.DataSource = ds;
if (TextBoxStreet.Text != "street")
{
LabelNoMatch.Visible = true; //this error message is showing
}
else
{
ListBoxSearchCust.DataTextField = "ID" + " " + "lastname" + ", " + "firstname";
ListBoxSearchCust.DataBind();
}
}
else
{
LabelNoStreet.Visible = true;
}
}

Report back your street value..
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 14:33:35
Street value shows to be "" in (TextBoxStreet.Text != null)
So - somehow the value I put in TextBoxStreet disappears when I click the button. I don't have any code in the buttonclick that clears the textbox (as seen above).
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-14 : 14:44:23
I found what caused it - and now I'm embarrased... There was code for clearing textboxes in the pageload. Thanks anyway all who helped!
Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-15 : 04:15:12
That leaves problem no2, how to turn it into parameters. I'm still not sure how to use them correctly. For example, when I try this I get the error message "invalid column 'name of street the user put in textbox'":

string sql = "SELECT ID, firstnamen, lastname FROM Customer WHERE street=" + @Street;
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add("@Street", SqlDbType.VarChar);
comm.Parameters["@Street"].Value = Street;
using (SqlDataReader dr = comm.ExecuteReader()) //error message here
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-15 : 05:16:15
A wild guess.Change your sql statement to this
string sql = "SELECT ID, firstname, lastname FROM Customer WHERE street='" + @Street+ "'";


PBUH

Go to Top of Page

KarinElvira
Starting Member

47 Posts

Posted - 2010-10-19 : 04:12:51
Thanks, I know newbies are a pain in everybodys *ss*s ;)
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-19 : 04:19:51
quote:
Originally posted by KarinElvira

Thanks, I know newbies are a pain in everybodys *ss*s ;)



So did it work?

PBUH

Go to Top of Page
    Next Page

- Advertisement -