Click here to Skip to main content
14,265,784 members
Rate this:
Please Sign up or sign in to vote.
See more:
Public Function
not working from class when i use a class and connection then it is not working
Public Function connection()

        con = New SqlConnection("server=DESKTOP-BNDMR5K;database=irfanansari;integrated security=true")
        Return con
    End Function


Public Function getvalueto() As SqlCommand


      com = New SqlCommand("select * from sale", Me.con)

      adpt = New SqlDataAdapter
      adpt.SelectCommand = com
      table = New DataTable

      adpt.Fill(table)
      MsgBox("Record Save Sussfully Now", MsgBoxStyle.Information, "Saved Record")




  End Function


not working when i use like this

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

'GridView1.DataSource = c.getvalueto
       'GridView1.DataBind()


End Sub


What I have tried:

<pre>Public Function connection()

        con = New SqlConnection("server=DESKTOP-BNDMR5K;database=irfanansari;integrated security=true")
        Return con
    End Function


Public Function getvalueto() As SqlCommand


      com = New SqlCommand("select * from sale", Me.con)

      adpt = New SqlDataAdapter
      adpt.SelectCommand = com
      table = New DataTable

      adpt.Fill(table)
      MsgBox("Record Save Sussfully Now", MsgBoxStyle.Information, "Saved Record")




  End Function


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

'GridView1.DataSource = c.getvalueto
       'GridView1.DataBind()


End Sub
Posted
Updated 3 days ago
Comments
MadMyche 3 days ago
   
Any errors being thrown? Have you debugged this and looked at the values step-by-step?
Richard Deeming 2 days ago
   
NB: You've tagged this as ASP.NET, but you're using MsgBox. That will not work. Remember, your code is running on the server.

At best, the code will throw an exception telling you that you can't display a user interface in a non-interactive program. At worst, the message will pop up on the server, where nobody will ever see it, and your code will hang waiting for an administrator to log in to the server and acknowledge thousands of messages.

It might appear to work when you debug the code in Visual Studio. But that's only because, in that specific case, the client and the server are the same computer. As soon as you deploy the code to a real server, the code will stop working.
irfanansari yesterday
   
i told already no error but Gridview not show any record and when i use same code behind form load its works it is not error not problem just it is not working as function

GridView1.DataSource = c.getvalueto
'GridView1.DataBind()

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

See the examples here: Function Statement (Visual Basic) | Microsoft Docs[^]
e.g.
Function MyFunction(ByVal j As Integer) As Double
    Return 3.87 * j
End Function
   
Comments
irfanansari yesterday
   
sir i use this
Public Function getvalueto() As SqlCommand


com = New SqlCommand("select * from sale", Me.con)

adpt = New SqlDataAdapter
adpt.SelectCommand = com
table = New DataTable

adpt.Fill(table)
MsgBox("Record Save Sussfully Now", MsgBoxStyle.Information, "Saved Record")




End Function

and behind load form

GridView1.DataSource = c.getvalueto
'GridView1.DataBind()

but not show any resuld not bound but as i use same code behind form load then it works
RickZeeland yesterday
   
Your function is not returning anything, use the Return statement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100