Stored Procedure

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Hi Ed,

Use SqlDataAdapter, something like:
SqlDataADapter adp = new SqlDataAdapter(objCmd);
DataTable table = new DataTable();
adp.Fill(table);
 
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.
__________________________________
 
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
 
Back
Top