Stored Procedure

G

Guest

hi,
I have a Stored Procedure in the SQL Server, called prCustomers it is
"select * from customers" - very simple
How am I able to execute the stored procedure get a result set and put the
first record of the result into a single textbox called last name

e.g.
objConnection = New SqlConnection(strConnection)
objConnection.Open()
objCmd = New SqlCommand
objCmd.Connection = objConnection
objCmd.CommandType = CommandType.StoredProcedure
objCmd.CommandText = "prCustomers"
??? objCmd.ExecuteReader(CommandBehavior.SingleResult)

LastName.text = ???

Thanks
Ed
 
M

Miha Markic [MVP C#]

Hi Ed,

Use SqlDataAdapter, something like:
SqlDataADapter adp = new SqlDataAdapter(objCmd);
DataTable table = new DataTable();
adp.Fill(table);
 
W

William \(Bill\) Vaughn

You're close.

Dim dr as SqlDataReader

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dr.Read
text1.text = dr.GetValue(0).ToString

(from memory)
Sure, it's easier to use Fill to build a DataTable, and then you can extract
the data from the Table rows collection.

I describe both techniques in my book.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

Ed
you can change the query to "select top 1 * from customers" in the stored
procedure. This will reduce the unnecessary overhead of getting all the
records
SalDataReader sd = objCmd.ExecuteReader(CommandBehavior.SingleResult);
if (sd.Read())
LastName.text = sd.GetString(0);
will get the name. Change the column index as necessary.
- sriram
 

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