ARTICLE

How to calll stored procedure in ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 11, 2011
Here we will see how to create a stored procedure in database and calling stored procedure from database to asp.net using vb language.
Download Files:
 
Reader Level:

Stored procedure

A stored procedure is a batch of Transact-SQL statements compiled into a single execution and then reutilizes the execution plan. when we write SQL statements, like select, inserts, updates to access your data from database. If you find yourself using the same query over and over again, it would make sense to put it into a stored procedure.Every time you write a query it is parsed in database. If you have written a stored procedure for it, it will be compiled once and can be executed  number of times.Stored procedures can also improve performance.

Step-1

Creating a table in database

create table logintab

(

username varchar(50),

password varchar(40)

)

Step-2

Creating Stored procedure

create procedure sploginproc

(

@username varchar(40),

@password varchar(20)

)

as

insert into logintab values(@username,@password )

Step-3

Calling stored procedure in VB.NET

Taking two TextBox, one Button and Label control on the form.

f1.gif

Figure 1.gif

Now double click on the Save button control and add the following code.

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

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

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        Dim com As SqlCommand = New SqlCommand("sploginproc", con)

        com.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBox1.Text

        com.Parameters.Add("@Password", SqlDbType.VarChar).Value = TextBox2.Text

        com.CommandType = CommandType.StoredProcedure

        con.Open()

        com.ExecuteNonQuery()

        Label1.Text = "record has been saved"

        con.Close()

    End Sub

End Class

 

Now run the application and enter the username and password.

f2.gif

Figure 2.gif

Now click on the save Button.

f3.gif

Figure 3.gif

Now open the database and test it.

f4.gif

Figure4.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor