First you create a class, wirte an Insert method passing an object as a parameter.
public class Insert
{
public string FirstName { get; set; }
public string Salary { get; set; }
public string Gender { get; set; }
//Connection string to connect to database
string ConnectionString = "Server=sa;Database=xyz;User Id=sa; password=sa123";
public void insertmethod(Insert add)
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
//Create the SqlCommand object
SqlCommand cmd = new SqlCommand("spAddEmployee", con);
//Specify that the SqlCommand is a stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd.Parameters.AddWithValue("@Name", add.FirsName);
cmd.Parameters.AddWithValue("@Gender", add.Gender);
cmd.Parameters.AddWithValue("@Salary", add.Salaray);
//Open the connection and execute the query
con.Open();
cmd.ExecuteNonQuery();
}
}
}
<table style="border: 1px solid black; font-family:Arial">
<tr>
<td>
Employee Name
</td>
<td>
<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gender
</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
//code behind of Aspx.cs
//Now create an object of Insert class
Insert add=new Insert();
protected void btnSubmit_Click(object sender, EventArgs e)
{
add.FirstName=txtEmployeeName.Text;
add.Gender=ddlGender.SelectedItem.Text;
add.Salary=txtSalary.Text;
//calling add method and passing object
add.insertmethod(add);
}