Call Function From Class?
Call Function From Class?
Call Function From Class?
Call Function From Class?
Call Function From Class?
Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class?
Call Function From Class? Call Function From Class?
Call Function From Class?
Go Back  Xtreme Visual Basic Talk > > > Call Function From Class?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-18-2014, 06:00 PM
Rabastan Rabastan is offline
Newcomer
 
Join Date: Aug 2012
Posts: 3
Default Call Function From Class?


I am just getting into using databases. I have created a class that seem to work. I have here a function I created with a tutorial. it is part of a DataAccess.class file. What I am confused about is how to;

A) Include the DataAccess.class file in the form I am working on
and
B)Call the Insert Function with a Button

Here is the code

Code:
Public Shared Function InsertNewRecord(ByVal item1 As String, ByVal item2 As String, ByVal item3 As String) As Boolean
	'Create the objects we need to insert a new record
	Dim cnInsert As New OleDbConnection(GetConnectionString("YourConnName"))
	Dim cmdInsert As New OleDbCommand
	Dim query As String = "INSERT INTO YourTable(column1,column2,column3) VALUES(@item1,@item2,@item3)"
	Dim iSqlStatus As Integer

	'Clear any parameters
	cmdInsert.Parameters.Clear()
	Try
	   'Set the OleDbCommand Object Properties
	   With cmdInsert
		  'Tell it what to execute
		  .CommandText = query 
		  'Tell it its a text query
		  .CommandType = CommandType.Text 
		  'Now add the parameters to our query
		  'NOTE: Replace @value1.... with your parameter names in your query
		  'and add all your parameters in this fashion
		  .Parameters.AddWithValue("@value1", item1)
		  .Parameters.AddWithValue("@value2", item2)
		  .Parameters.AddWithValue("@value3", item3)
		  'Set the connection of the object
		  .Connection = cnInsert
	  End With

	  'Now take care of the connection
	  HandleConnection(cnInsert)

	  'Set the iSqlStatus to the ExecuteNonQuery 
	  'status of the insert (0 = failed, 1 = success)
	  iSqlStatus = cmdInsert.ExecuteNonQuery

	  'Now check the status
	  If Not iSqlStatus = 0 Then
		  'DO your failed messaging here
		  Return False
	  Else
		 'Do your success work here
		  Return True
	  End If
	Catch ex As Exception
	   MsgBox(ex.Message, "Error")
	Finally
		'Now close the connection
		HandleConnection(cnInsert)
	End Try
End Function
Reply With Quote
  #2  
Old 02-18-2014, 08:48 PM
dilettante's Avatar
dilettanteCall Function From Class? dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

Wrong forum. VB.Net questions belong in the VB.Net forums.
Reply With Quote
  #3  
Old 02-19-2014, 09:36 AM
PlausiblyDamp's Avatar
PlausiblyDampCall Function From Class? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You wouldn't include the class in the form, you would create a variable whose type is your data access class an create a new instance. Then you call the function from this variable.

e.g.
Assuming your data class is just called DataClass you would do something like the following behind a button click in your form.
Code:
Dim db as New DataClass()
dim test = db.InsertNewRecord("stuff", "more stuff", "even more stuff")
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 02-19-2014, 10:13 AM
Gruff's Avatar
GruffCall Function From Class? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Classes are blueprints. To use them in your program you have to create an object variable from the class. (An instance)

Assuming your function is correct you create an instance of your class on the button click then call the method (Function) from the instance.

Code:
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'No error checking shown in this sample ' Create an instance of your class. Dim oDataAccess As New DataAccess Dim Success As Boolean = oDataAccess.InsertNewRecord(Textbox1.text, TextBox2.text, TextBox3.text) If Success Then MessageBox.Show("Items inserted") Else MessageBox.Show("Failed to insert Items") End If End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 02-23-2014 at 05:45 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Call Function From Class?
Call Function From Class?
Call Function From Class? Call Function From Class?
Call Function From Class?
Call Function From Class?
Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class? Call Function From Class?
Call Function From Class?
Call Function From Class?
 
Call Function From Class?
Call Function From Class?
 
-->