3 Replies - 634 Views - Last Post: 03 May 2008 - 06:48 PM Rate Topic: -----

#1 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Simple SQL question

Post icon  Posted 03 May 2008 - 06:10 PM

Hello All!

I have a simple question. I know how to do this, just can't remember exactly how to write it out. I don't have access to my notes, or books, and can't seem to find this on the web. So here it goes.

I have a store procedure as a simple select statement.
SELECT	 VisName
FROM		 tblVis
WHERE	 (VisID = 1)


He is my code.
 Dim constr As New SqlConnection(PVDBConn)
		Try
			cmdUpd = New SqlCommand("SelVis1Name", constr)
			cmdUpd.CommandType = CommandType.StoredProcedure
					constr.Open()
						cmdUpd.ExecuteNonQuery()
			constr.Close()
		Catch ex As Exception
			MsgBox(ex.Message.ToString)
		End Try


I want to pass that one value to the property of the button.text property.
Can anybody jog my memory and remind how I pass that result from the SP to that property?

TIA!

Rudy

Is This A Good Question/Topic? 0
  • +

Replies To: Simple SQL question

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Simple SQL question

Posted 03 May 2008 - 06:40 PM

First, ExecuteNonQuery() isnt what you want, that doesnt return any values except the number of rows affected in an UPDATE, INSERT and DELETE statement. What you want is ExecuteScalar() since you're only returning a single value. Do these modiciations to your code


 Dim constr As New SqlConnection(PVDBConn)
        Try
            'Variable to hold the results
            Dim results As String = String.Empty
            cmdUpd = New SqlCommand("SelVis1Name", constr)
            cmdUpd.CommandType = CommandType.StoredProcedure
            constr.Open()
            'Set results to the value returned from ExecuteScalar()
            results = CType(cmdUpd.ExecuteScalar(), String)
            constr.Close()
            'Set our buttons text to that value
            Button1.Text = results
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try


Was This Post Helpful? 0
  • +
  • -

#3 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Re: Simple SQL question

Posted 03 May 2008 - 06:47 PM

View PostPsychoCoder, on 3 May, 2008 - 06:40 PM, said:

First, ExecuteNonQuery() isnt what you want, that doesnt return any values except the number of rows affected in an UPDATE, INSERT and DELETE statement. What you want is ExecuteScalar() since you're only returning a single value. Do these modiciations to your code


 Dim constr As New SqlConnection(PVDBConn)
        Try
            'Variable to hold the results
            Dim results As String = String.Empty
            cmdUpd = New SqlCommand("SelVis1Name", constr)
            cmdUpd.CommandType = CommandType.StoredProcedure
            constr.Open()
            'Set results to the value returned from ExecuteScalar()
            results = CType(cmdUpd.ExecuteScalar(), String)
            constr.Close()
            'Set our buttons text to that value
            Button1.Text = results
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try




Perfect man!!

Thanks!

Rudy
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Simple SQL question

Posted 03 May 2008 - 06:48 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1