1. Open Visual Studio
2. Create a New Website
3. From Website - > Add New Item -> SQL Server Database -> Add.
4. From Server Explorer Expand your Database -> Right Click - > Add New Table.
13. Click F5.
2. Create a New Website
3. From Website - > Add New Item -> SQL Server Database -> Add.
4. From Server Explorer Expand your Database -> Right Click - > Add New Table.
5. From Database - > Select Stored Procedure -> Right Click - > Add New Stored Procedure.
6. Create Insert Query for Employee Table.
7. Add connection string in Web.Config File.
<connectionStrings>
<add name="conn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Asp.net_Controls\App_Data\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
8. From Website -> Add New Item -> Class -> Give name -> Click Add.
9. Add namespaces to Class File.
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
10. Create a method in class for the insert.
static string str = WebConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection con = new SqlConnection(str);
int i;
public int insert_Employee(string EmpName, string EmpAddress, string EmpPhone, string EmpMobile, string EmpSalary, string EmpPassport)
{
try
{
SqlCommand cmd = new SqlCommand("insert_Employee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EmpName", EmpName);
cmd.Parameters.AddWithValue("@EmpAddress", EmpAddress);
cmd.Parameters.AddWithValue("@EmpPhone", EmpPhone);
cmd.Parameters.AddWithValue("@EmpMobile", EmpMobile);
cmd.Parameters.AddWithValue("@EmpSalary", EmpSalary);
cmd.Parameters.AddWithValue("@EmpPassport", EmpPassport);
i = cmd.ExecuteNonQuery();
return i;
}
catch (Exception ex)
{
return i = 0;
}
}
11. Add web Page. and Design UI.
12. Write Code on Button click event.
mainClass objMain = new mainClass();
int i;
protected void Button1_Click(object sender, EventArgs e)
{
i = objMain.insert_Employee(txtname.Text, txtadress.Text, txtphone.Text, txtmobile.Text, txtsalry.Text, txtpassport.Text);
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Successfully inserted!');</script>");
}
else
{
Response.Write("<script language='javascript'>alert('Error try again!');</script>");
}
}
No comments:
Post a Comment
Thanks