Hi nauna,
Please refer below sample.
SQL
----Table Creation----------------------
CREATE TABLE TestDemo
(
Id UNIQUEIDENTIFIER PRIMARY KEY default NEWID(),
Name VARCHAR (50),
Country VARCHAR(50)
)
-----Stored Procedure-----------------
ALTER PROCEDURE AddTestDemoID
@Name varchar(50),
@Country varchar(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TestDemo (Name,Country)
OUTPUT inserted.Id
VALUES (@Name, @Country)
END
HTML
<div>
Name
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void btnInsert_Click(object sender, EventArgs e)
{
String strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strconstr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "AddTestDemoID";
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.Trim();
cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim();
cmd.Connection = con;
try
{
con.Open();
Guid id = (Guid)cmd.ExecuteScalar();
lblMessage.Text = "Record inserted successfully. ID = " + id;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
}
VB.Net
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(strconstr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddTestDemoID"
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.Trim()
cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim()
cmd.Connection = con
Try
con.Open()
Dim id As Guid = CType(cmd.ExecuteScalar(), Guid)
lblMessage.Text = "Record inserted successfully. ID = " & id.ToString
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Using
End Using
End Sub
Screenshot