Wednesday, March 7, 2012

Assign a variable based upon query results...how to do it?

I have the following code which is incomplete. Where it says:
txtVendorID =
I need it to equal the results of the field VendorID from my query...here is my code. What do I need to add there?

Dim cmdSelectAs SqlCommand
Dim intRecordID
intRecordID = Request.QueryString("RecordID")
strConn = ConfigurationManager.AppSettings("conn")
conn =New SqlConnection(strConn)
cmdSelect =New SqlCommand("spMfgRepListAddaspxByRecordID", conn)
cmdSelect.CommandType = CommandType.StoredProcedure
cmdSelect.Parameters.AddWithValue("@.RecordID", intRecordID)
conn.Open()cmdSelect.ExecuteReader()
txtVendorID.Text =
conn.Close()If VendorID is the only field coming back from your SP (ie SELECT VendorID FROM...) then you can do

txtVendorID.Text = cmdSelect.ExecuteScalar().ToString(); // Might have a problem with NULL's if the SP returns no results so guard against this

Otherwise you have to use

SqlDataReader dr = cmdSelect.ExecuteReader();

if ( dr.Read() )
txtVendorID.Text = dr["VendorID"].ToString(); // Assumes VendorID is the name of your field|||

hi.

you need to use datareader as;

Dim

readerAs SqlDataReader

reader = cmd.ExecuteReader()

reader.Reade()

txtVendorID.Text =reader(

"VendorID").ToString() ' VendorID which retrive from the database.

No comments:

Post a Comment