Click here to Skip to main content
15,909,651 members
Please Sign up or sign in to vote.
1.00/5 (1 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 14-Aug-19 9:05am
Comments
MadMyche 14-Aug-19 14:38pm    
Any errors being thrown? Have you debugged this and looked at the values step-by-step?
Richard Deeming 15-Aug-19 14:09pm    
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 16-Aug-19 6:00am    
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

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
 
Share this answer
 
Comments
irfanansari 16-Aug-19 6:04am    
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 16-Aug-19 7:26am    
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900