Hi KALEEM,
I have created sample please check below.
SQL
CREATE PROCEDURE [dbo].[Customers_CRUD]
@Action VARCHAR(10)
,@CustomerId INT = NULL
,@Name VARCHAR(100) = NULL
,@Country VARCHAR(100) = NULL
AS
BEGIN
SET NOCOUNT ON;
--SELECT
IF @Action = 'SELECT'
BEGIN
SELECT CustomerId, Name, Country
FROM Customers
END
--INSERT
IF @Action = 'INSERT'
BEGIN
INSERT INTO Customers(Name, Country)
VALUES (@Name, @Country)
END
--UPDATE
IF @Action = 'UPDATE'
BEGIN
UPDATE Customers
SET Name = @Name, Country = @Country
WHERE CustomerId = @CustomerId
END
--DELETE
IF @Action = 'DELETE'
BEGIN
DELETE FROM Customers
WHERE CustomerId = @CustomerId
END
END
HTML
<div>
<table>
<tr>
<td>
Action
</td>
<td>
<asp:DropDownList runat="server" placeholder="Enter Action" ID="ddlAction">
<asp:ListItem Text="-Select Action-" />
<asp:ListItem Text="Select" />
<asp:ListItem Text="Insert" />
<asp:ListItem Text="Update" />
<asp:ListItem Text="Delete" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
ID
</td>
<td>
<asp:TextBox runat="server" ID="txtCustomerId" name="id" placeholder="Enter CustomerId"
type="text"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox runat="server" ID="txtCustomerName" name="name" placeholder="Enter Customer Name"
type="text"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox runat="server" ID="txtCustomerCountry" class="country" placeholder="Enter Customer Country"
type="text"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button Text="submit" runat="server" OnClick="Submit_data" />
</td>
</tr>
</table>
</div>
<br />
<asp:GridView ID="gvAcademics" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Code
AcademicDataContext context = new AcademicDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
gvAcademics.DataSource = context.Customers_CRUD("SELECT", null, string.Empty, string.Empty);
gvAcademics.DataBind();
}
}
protected void Submit_data(object sender, EventArgs e)
{
string action = ddlAction.SelectedItem.Text.Trim().ToUpper();
if (action == "SELECT")
{
context.Customers_CRUD(action, null, string.Empty, string.Empty);
}
else if (action == "INSERT")
{
context.Customers_CRUD(action, null, txtCustomerName.Text.Trim(), txtCustomerCountry.Text.Trim());
}
else if (action == "UPDATE")
{
context.Customers_CRUD(action, int.Parse(txtCustomerId.Text.Trim()), txtCustomerName.Text.Trim(), txtCustomerCountry.Text.Trim());
}
else if (action == "DELETE")
{
context.Customers_CRUD(action, int.Parse(txtCustomerId.Text.Trim()), txtCustomerName.Text.Trim(), txtCustomerCountry.Text.Trim());
}
context.SubmitChanges();
gvAcademics.DataSource = context.Customers_CRUD("SELECT", null, string.Empty, string.Empty);
gvAcademics.DataBind();
}
Screenshot
