locked
SQL Statement in VB project codebehind RRS feed

  • Question

  • User-1139693770 posted

    So in my codebehind file for a main menu, I have a method to execute an sql command to select a name from a certain database, given that the user entered a valid userid on the previous login page. So example, I enter my ID 1604, and hit enter. That value is passed to the next page and displayed at the top.

    I have an SQL statement execute on this 2nd page to select the name from the DB, but how do I display it in a form?

     

     

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
            Dim strConnString As String = ConfigurationManager.ConnectionStrings("PartsConnectionString").ConnectionString
            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand("SELECT FullName FROM ImportTest WHERE IDnum = @IDnum")
            cmd.Parameters.Add("@IDnum", SqlDbType.VarChar).Value = Session("ID").ToString()
            cmd.Connection = con
            Try
                con.Open()
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
            Session.Remove("ID")
    
        End Sub
    
    
    
    End Class
    
     
    The code from the Login page is here :
     
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
    
    
        Protected Sub Login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login_Button.Click
            Session("ID") = Login_Textbox.Text
            Server.Transfer("Main_Menu.aspx")
            Response.Redirect("~/Main_Menu.aspx")
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Login_Textbox.Focus()
        End Sub
    
    
    
    
    End Class
    
    Friday, June 10, 2011 12:35 PM

Answers

  • User-1028151323 posted

    you want to set a variable like

    dim fullName as string before your SQL

    then replace

    cmd.ExecuteNonQuery()

    with
    fullName = cmd.ExecuteScalar()

    Then if you have a label on the page (lblResult)

    fullName = cmd.ExecuteScalar()
    lblResult.text = fullName

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim fullName as String
           
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("PartsConnectionString").ConnectionString
           
    Dim con As New SqlConnection(strConnString)
           
    Dim cmd As New SqlCommand("SELECT FullName FROM ImportTest WHERE IDnum = @IDnum")
            cmd
    .Parameters.Add("@IDnum", SqlDbType.VarChar).Value = Session("ID").ToString()
            cmd
    .Connection = con
           
    Try
                con
    .Open()
                fullName = cmd
    .ExecuteScalar()
     lblResult.text = fullName
           
    Catch ex As Exception
               
    Throw ex
           
    Finally
                con
    .Close()
                con
    .Dispose()
           
    End Try
           
    Session.Remove("ID")

       
    End Sub


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 1:54 PM