Open the Visual Studio and select ASP.NET WebForm Empty project template.
Add ASP.NET WebForm as an item
Design the WebForm1.aspx page as shown below and do the coding as given ahead. Insert data in the text box controls and click the Submit button to save in SQL Server database.
Data is saved in the database successfully
Data is saved in the database can be verified in the SSMS.
The ConnectionString required to establish connection with SQL Server is given in Web.config file.
C# ASP.NET CRUD Part1 :
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
namespace CRUD_ASP
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write("Is not PostBack");
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
string firstname = FirstNameTextBox.Text;
string lastname = LastNameTextBox.Text;
string email = EmailTextBox.Text;
string constring = ConfigurationManager.ConnectionStrings["csx"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constring))
{
string query = "INSERT INTO tbl_Employee (FirstName,LastName,Email) VALUES( @FirstName, @LastName, @Email)";
using (SqlCommand command = new SqlCommand(query, conn))
{
command.Parameters.AddWithValue("@FirstName", firstname);
command.Parameters.AddWithValue("@LastName", lastname);
command.Parameters.AddWithValue("@Email", email);
conn.Open();
command.ExecuteNonQuery();
Response.Write("Data inserted successfully");
}
}
}
}
}
© अजीत कुमार, सर्वाधिकार सुरक्षित।
इस आलेख को उद्धृत करते हुए इस लेख के लिंक का भी विवरण दें। इस आलेख को कॉपीराइट सूचना के साथ यथावत साझा करने की अनुमति है। कृपया इसे ऐसे स्थान पर साझा न करें जहाँ इसे देखने के लिए शुल्क देना पडे।
No comments:
Post a Comment