help!!i'm stuck here!!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys,
May i know how to match the value in the text box with the table's attribute
in SQL Server? For example, when the customer enter their customer id and
press the button, I want to display out the rows of that customer and not the
whole table. I had done the data binding in the text box with the customer ID
and I don't know whether it is correct or not.
Thanks for ur help..any sample code will be appreciated.
 
Hi!

Well, you might use sql script here.
select value1, value2 from customertable where ID = customerID
To do this you can use a datareader and get the value to the textbox.

/Marre
 
can u explain more? i will appreciate it if u can provide me with a step ny
step instruction..i'm a new beginner here...thanks..
 
Ok.

First you have to connect to the sql server:
SqlConnection conn = new SqlConnection(youreConnectionstring);

then you have to run the sql script:
SqlCommand myCommand = new SqlCommand("select value1, value2 from myTable
where ID =" + youreID, conn);

and put it in a datareader:
SqlDataReader myReader = myCommand.ExecuteReader();

To get the text into textboxes you can do like this:

while(myReader.Read())
{
textbox1.Text = myReader["value1"].ToString();
textbox2.Text = myReader["value2"].ToString();
}
 
what is value1 and value2 mean? is it Customer_ID? and what is myID means?
Do i have to type it in the button? thanks
MA said:
Ok.

First you have to connect to the sql server:
SqlConnection conn = new SqlConnection(youreConnectionstring);

then you have to run the sql script:
SqlCommand myCommand = new SqlCommand("select value1, value2 from myTable
where ID =" + youreID, conn);

and put it in a datareader:
SqlDataReader myReader = myCommand.ExecuteReader();

To get the text into textboxes you can do like this:

while(myReader.Read())
{
textbox1.Text = myReader["value1"].ToString();
textbox2.Text = myReader["value2"].ToString();
}

alyssa said:
can u explain more? i will appreciate it if u can provide me with a step
ny
step instruction..i'm a new beginner here...thanks..
 
Replace value1 and value2 with the columns from the database table that you
want.
If you have columns called customerName and Customer_ID in a table called
customer, you write:
"select customerName from customer where Customer_ID = "+youreID
where youreID is the ID of the customer (get this from an input field
maybe?).

And you can use this code in the buttons click event.

/Marre
alyssa said:
what is value1 and value2 mean? is it Customer_ID? and what is myID means?
Do i have to type it in the button? thanks
MA said:
Ok.

First you have to connect to the sql server:
SqlConnection conn = new SqlConnection(youreConnectionstring);

then you have to run the sql script:
SqlCommand myCommand = new SqlCommand("select value1, value2 from myTable
where ID =" + youreID, conn);

and put it in a datareader:
SqlDataReader myReader = myCommand.ExecuteReader();

To get the text into textboxes you can do like this:

while(myReader.Read())
{
textbox1.Text = myReader["value1"].ToString();
textbox2.Text = myReader["value2"].ToString();
}

alyssa said:
can u explain more? i will appreciate it if u can provide me with a
step
ny
step instruction..i'm a new beginner here...thanks..

:

Hi!

Well, you might use sql script here.
select value1, value2 from customertable where ID = customerID
To do this you can use a datareader and get the value to the textbox.

/Marre
Hi guys,
May i know how to match the value in the text box with the table's
attribute
in SQL Server? For example, when the customer enter their customer
id
and
press the button, I want to display out the rows of that customer
and
not
the
whole table. I had done the data binding in the text box with the
customer
ID
and I don't know whether it is correct or not.
Thanks for ur help..any sample code will be appreciated.
 
errmm....this prog works like this...it is a summary report form where
there's a text box calling the user to enter a customer id and click the
button. then, there's a data grid displaying all the particulars about that
customer id only..so i don't understand what's 'yourid' means and how do i
get this from the text box?sorry,i'm a bit slow here...how to display the
result in the data grid?thanks

MA said:
Replace value1 and value2 with the columns from the database table that you
want.
If you have columns called customerName and Customer_ID in a table called
customer, you write:
"select customerName from customer where Customer_ID = "+youreID
where youreID is the ID of the customer (get this from an input field
maybe?).

And you can use this code in the buttons click event.

/Marre
alyssa said:
what is value1 and value2 mean? is it Customer_ID? and what is myID means?
Do i have to type it in the button? thanks
MA said:
Ok.

First you have to connect to the sql server:
SqlConnection conn = new SqlConnection(youreConnectionstring);

then you have to run the sql script:
SqlCommand myCommand = new SqlCommand("select value1, value2 from myTable
where ID =" + youreID, conn);

and put it in a datareader:
SqlDataReader myReader = myCommand.ExecuteReader();

To get the text into textboxes you can do like this:

while(myReader.Read())
{
textbox1.Text = myReader["value1"].ToString();
textbox2.Text = myReader["value2"].ToString();
}

can u explain more? i will appreciate it if u can provide me with a
step
ny
step instruction..i'm a new beginner here...thanks..

:

Hi!

Well, you might use sql script here.
select value1, value2 from customertable where ID = customerID
To do this you can use a datareader and get the value to the textbox.

/Marre
Hi guys,
May i know how to match the value in the text box with the table's
attribute
in SQL Server? For example, when the customer enter their customer
id
and
press the button, I want to display out the rows of that customer
and
not
the
whole table. I had done the data binding in the text box with the
customer
ID
and I don't know whether it is correct or not.
Thanks for ur help..any sample code will be appreciated.
 
Well. Youre not slow :)
use this sql string to do it:
"select customerName from customer where Customer_ID = "+textbox.text
Where the textbox is the input field from the customer.
Youre code would look like this if I made it :)

private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("col1", typeof(string)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));

SqlConnection conn = new SqlConnection(youreConnectionstring);
conn.Open();
SqlCommand myCommand = new SqlCommand("select value1, value2 from
myTable where ID =" + textbox1.text, conn);
SqlDataReader myReader = myCommand.ExecuteReader();

DataView dv;
DataRow dr;
while(myReader.Read())
{
dr = dt.NewRow();
dr[0] = myReader["value1"].ToString();
dr[1] = myReader["value2"].ToString();

dt.Rows.Add(dr);
}

myReader.Close();
conn.Close();

dv = new DataView(dt);
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}


/Marre
alyssa said:
errmm....this prog works like this...it is a summary report form where
there's a text box calling the user to enter a customer id and click the
button. then, there's a data grid displaying all the particulars about
that
customer id only..so i don't understand what's 'yourid' means and how do i
get this from the text box?sorry,i'm a bit slow here...how to display the
result in the data grid?thanks

MA said:
Replace value1 and value2 with the columns from the database table that
you
want.
If you have columns called customerName and Customer_ID in a table called
customer, you write:
"select customerName from customer where Customer_ID = "+youreID
where youreID is the ID of the customer (get this from an input field
maybe?).

And you can use this code in the buttons click event.

/Marre
alyssa said:
what is value1 and value2 mean? is it Customer_ID? and what is myID
means?
Do i have to type it in the button? thanks
:

Ok.

First you have to connect to the sql server:
SqlConnection conn = new SqlConnection(youreConnectionstring);

then you have to run the sql script:
SqlCommand myCommand = new SqlCommand("select value1, value2 from
myTable
where ID =" + youreID, conn);

and put it in a datareader:
SqlDataReader myReader = myCommand.ExecuteReader();

To get the text into textboxes you can do like this:

while(myReader.Read())
{
textbox1.Text = myReader["value1"].ToString();
textbox2.Text = myReader["value2"].ToString();
}

can u explain more? i will appreciate it if u can provide me with a
step
ny
step instruction..i'm a new beginner here...thanks..

:

Hi!

Well, you might use sql script here.
select value1, value2 from customertable where ID = customerID
To do this you can use a datareader and get the value to the
textbox.

/Marre
Hi guys,
May i know how to match the value in the text box with the
table's
attribute
in SQL Server? For example, when the customer enter their
customer
id
and
press the button, I want to display out the rows of that customer
and
not
the
whole table. I had done the data binding in the text box with the
customer
ID
and I don't know whether it is correct or not.
Thanks for ur help..any sample code will be appreciated.
 
MA said:
Well. Youre not slow :)
use this sql string to do it:
"select customerName from customer where Customer_ID = "+textbox.text
Where the textbox is the input field from the customer.
Youre code would look like this if I made it :)

That has then introduced a SQL injection attack possibility.

Instead of directly inserting the value into the SQL, you should use a
parameterised query.

See http://www.pobox.com/~skeet/csharp/faq/#db.parameters
 
Back
Top