Saturday, March 24, 2012

Please help how to call stored procedure from asp.net code

Please help, first time trying to use the stored procedure.

Can you please modify my code below which is in asp.net to insert the record's in table(tbl_labels), i don't have not problem in inserting records, but i want to use my stored procedure to insert the record. Please help I never used a stored procedure.

**********


CREATE PROCEDURE sp_insert_label
(
@dotnet.itags.org.engl nvarchar,
@dotnet.itags.org.espl nvarchar,
@dotnet.itags.org.frlbl nvarchar,
@dotnet.itags.org.gerlbl nvarchar
)
AS
INSERT INTO tbl_labels
(
eng_lbl,
esp_lbl,
fr_lbl,
ger_lbl
)
VALUES
(
@dotnet.itags.org.engl,
@dotnet.itags.org.espl,
@dotnet.itags.org.frlbl,
@dotnet.itags.org.gerlbl
)
GO

********** the following is my asp.net code***

Sub Addlabel_Click(Sender As Object, E As EventArgs)
Page.Validate()
If Not Page.IsValid
Return
End If

Dim DS As DataSet
Dim MyCommand As SqlCommand
Dim InsertCmd As String = "insert into tbl_labels(eng_lbl, esp_lbl, " & _
"fr_lbl, ger_lbl) values " & _
"(@dotnet.itags.org.engl, @dotnet.itags.org.espn, @dotnet.itags.org.fren, @dotnet.itags.org.german)"

MyCommand = New SqlCommand(InsertCmd, MyConnection)

MyCommand.Parameters.Add( "@dotnet.itags.org.engl", eng_lbl.Value )
MyCommand.Parameters.Add( "@dotnet.itags.org.espn", esp_lbl.Value )
MyCommand.Parameters.Add( "@dotnet.itags.org.fren", fr_lbl.Value )
MyCommand.Parameters.Add( "@dotnet.itags.org.german", ger_lbl.Value )

MyCommand.Connection.Open()

Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "Label Added Successfully to Dictionary.<br>" '& InsertCmd.ToString()
ger_lbl.Value = ""
fr_lbl.Value = ""
esp_lbl.Value = ""
eng_lbl.Value = ""

Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the " & _
"same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please ensure " & _
"the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try

MyCommand.Connection.Close()
BindGrid()
End Sub

Hi,

Dim DS As DataSet

Dim MyCommand As SqlCommand

Dim pmEngl, pmEspn, pmFren, pmGerman As SqlParameter = New SqlParameter()

MyCommand = New SqlCommand("sp_insert_label", MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure;

pmEngl = MyCommand.Parameters.Add( "@.engl", eng_lbl.Value )

pmEspn = MyCommand.Parameters.Add( "@.espn", esp_lbl.Value )

pmFren = MyCommand.Parameters.Add( "@.fren", fr_lbl.Value )

pmGerman = MyCommand.Parameters.Add( "@.german", ger_lbl.Value )

MyCommand.Connection.Open()

Try

MyCommand.ExecuteNonQuery()

Message.InnerHtml = "Label Added Successfully to Dictionary.<br>" '& InsertCmd.ToString()

ger_lbl.Value = ""

fr_lbl.Value = ""

esp_lbl.Value = ""

eng_lbl.Value = ""

hope it helps.
You can add parameters to stored procedures in different ways:

One of the useful way is:

myCommand.Parameters.Add(new SqlParameters("@.ParameterName",SqlDbType.nvarchar,50));
Thank you very much for your help Harish.

0 comments:

Post a Comment