locked
SqlDataSource.SelectParameters with variable or session from MySqlDataReader return nothing RRS feed

  • Question

  • User-1258024267 posted

    Sorry for my english !

    When I click on button 

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ReadMyData(ConfigurationManager.ConnectionStrings("myconnection").ConnectionString)

    End Sub

    After

    Public Sub ReadMyData(myConnString As String)

    Dim myVariable As String = ""

    Dim myConnection As New MySqlConnection(myConnString)
    Dim myCommand As New MySqlCommand("mystoreprocedure", myConnection)

    myCommand.CommandType = CommandType.StoredProcedure
    myCommand.Parameters.AddWithValue("@pMyparam", "9063")

    myConnection.Open()
    Dim myReader As MySqlDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()


    myVariable  = myReader.GetString(0)

    End While

    Session("mySession") = myVariable
    Me.SqlDataSource.SelectParameters(0).DefaultValue = Session("mySession")
    Me.SqlDataSource.DataBind()

    myReader.Close()
    myConnection.Close()

    End Sub 

    My SqlDataSource and my datalist is always get nothing, I check everthing my datareader return 1234 and  myVariable  = 123

    if I put like 
    Me.SqlDataSource.SelectParameters(0).DefaultValue = "1234" instead is work perfectly.

    Please help me , thank you 

    Saturday, April 4, 2015 1:35 PM

Answers

  • User281315223 posted

    Have you tried placing a breakpoint and actually stepping through this code as it executes? It could be possible that your actual value being returned isn't what is being expected :

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' This should be straight-forward, passing a connection string to a connection '
        ReadMyData(ConfigurationManager.ConnectionStrings("myconnection").ConnectionString)
    End Sub
    
    Public Sub ReadMyData(myConnString As String)
       Dim myVariable As String = ""
    
       Using myConnection As New MySqlConnection(myConnString)
          ' Open your connection '
          myConnection.Open()
     
          ' Build a command to execute using your stored procedure '
          Using myCommand As New MySqlCommand("mystoreprocedure", myConnection)
             ' Indicate this is a stored procedure '
             myCommand.CommandType = CommandType.StoredProcedure
    
             ' Add your parameters '
             myCommand.Parameters.AddWithValue("@pMyparam", "9063") 
    
             ' Read through your results '
             Using myReader = myCommand.ExecuteReader()
                 While myReader.Read()
                      ' This will continually overwrite the myVariable value. Is this what you want? '
                      myVariable  = myReader.GetString(0)
                 End While
             End Using
          End Using
          ' Update your data sources (why are you storing it in the Session here? Just use the myVariable value. '
          Session("mySession") = myVariable
          Me.SqlDataSource.SelectParameters(0).DefaultValue = myVariable
          Me.SqlDataSource.DataBind()
       End Using 
    End Sub 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2015 8:51 AM

All replies

  • User281315223 posted

    Have you tried placing a breakpoint and actually stepping through this code as it executes? It could be possible that your actual value being returned isn't what is being expected :

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' This should be straight-forward, passing a connection string to a connection '
        ReadMyData(ConfigurationManager.ConnectionStrings("myconnection").ConnectionString)
    End Sub
    
    Public Sub ReadMyData(myConnString As String)
       Dim myVariable As String = ""
    
       Using myConnection As New MySqlConnection(myConnString)
          ' Open your connection '
          myConnection.Open()
     
          ' Build a command to execute using your stored procedure '
          Using myCommand As New MySqlCommand("mystoreprocedure", myConnection)
             ' Indicate this is a stored procedure '
             myCommand.CommandType = CommandType.StoredProcedure
    
             ' Add your parameters '
             myCommand.Parameters.AddWithValue("@pMyparam", "9063") 
    
             ' Read through your results '
             Using myReader = myCommand.ExecuteReader()
                 While myReader.Read()
                      ' This will continually overwrite the myVariable value. Is this what you want? '
                      myVariable  = myReader.GetString(0)
                 End While
             End Using
          End Using
          ' Update your data sources (why are you storing it in the Session here? Just use the myVariable value. '
          Session("mySession") = myVariable
          Me.SqlDataSource.SelectParameters(0).DefaultValue = myVariable
          Me.SqlDataSource.DataBind()
       End Using 
    End Sub 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2015 8:51 AM
  • User-1258024267 posted

    Thanks a lot for your time, the was  wrong data from database : so many time lost :(  but I learn ... 

    Tuesday, April 7, 2015 9:24 PM