Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,911 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,194 people online right now. Registration is fast and FREE... Join Now!




connecting sql database with visual basic

 
Reply to this topicStart new topic

connecting sql database with visual basic, trying to connect my division form in visual basis with the insert pro

ayodapo
24 Jan, 2008 - 08:26 AM
Post #1

New D.I.C Head
*

Joined: 24 Jan, 2008
Posts: 1

CODE

  Import system
  Import system.data.sqlclient
dim conn as new  sqlconnection
private connection string as string=sqlconn.connection string
public function insertdivision(by val divisionID as string,by val divisionname as string)as boolean
dim val as int32
try
   dim param()as sqlparameter=(new sqlparameter("@divisionid'',division id),new sqlparameter("@divisionname",divisionname)
val=new sqlparameter.executenonquery(connectionstring,commandtype.stored procedure,"insertdivision",param)
return val
catch ex as sqlexception
throw ex
catch ex as exception
throw ex
end try
end function
end class


*edit: Please use code tags in the future. Thanks! code.gif

This post has been edited by Martyr2: 24 Jan, 2008 - 09:06 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Connecting Sql Database With Visual Basic
24 Jan, 2008 - 09:28 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
There are so many things wrong with this I don't even know where to begin. Or maybe I do....

1) Your syntax is horrible. It might have been the way you posted it but keywords like "ByVal" have no space in them. Same with "divisionid".

2) You attempt to use the connectionstring property of an object called sqlconn. Perhaps you mean conn, because that is what you defined.

3) Your array of parameters is chopped to bits. Remove the unneeded spaces, use a double quote to end the argument "@divisionid" (you are using two single quotes), if you are attempting to initalize you have to use something like....

CODE

Dim param() As SqlParameter = {New SqlParameter("@divisionid", divisionID), New SqlParameter("@divisionname", divisionname)}


Notice the use of the curly braces.

4) You attempt to use ExecuteNonQuery on a sqlparameter object. Perhaps you meant to use a sqlCommand object, which you also have not defined.

5) Perhaps you will build in something later, but if both the sqlexception and the exception catches are just going to throw, take out the sqlexception and let the exception catch do all the throwing.

Most of this could have been caught by your compiler. So use it! Then we can go tackling your connection problem. In the meantime, take a look at the site below for using a command object to pass in parameters to a stored procedure.

The Ins and Outs of Stored Procedures in VB.Net

Your specific piece of this article is under the title "Using the Parameters Collection to Pass Parameters To and From Stored Procedures"

Enjoy!


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 01:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month