How To Connect ASP.Net to SQL Server?(Visual Studio 2017 & MS SQL Server 2014)
Create Database-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emp.aspx.cs" Inherits="ASPCRUDOPERATION.Emp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td>Eid</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Ename</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Job</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Salary</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>DepartNo</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem Selected="True">---Selecte Department---</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>Status</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Active" />
</td>
</tr>
<tr>
<td>Photo</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Update" OnClick="Button2_Click" />
</td>
<td>
<asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="Display" OnClick="Button4_Click" />
</td>
</tr>
</table>
<div>
<asp:GridView ID="GridView1" runat="server" align="center" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</div>
</form>
</body>
</html>
C# Coding-
using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPCRUDOPERATION
{
public partial class Emp : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataSet ds;
string strname;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Database=InsertData;Data Source=DESKTOP-VHR35FE\\PK22KAR;Integrated security=SSPI");
cmd = new SqlCommand();
cmd.Connection = con;
}
private void ShowData()
{
if (FileUpload1.HasFile)
{
strname = FileUpload1.FileName.ToString();
FileUpload1.PostedFile.SaveAs(Server.MapPath("") + strname);
}
cmd.Parameters.AddWithValue("@Eid", TextBox1.Text);
cmd.Parameters.AddWithValue("@Ename", TextBox2.Text);
cmd.Parameters.AddWithValue("@Job", TextBox3.Text);
cmd.Parameters.AddWithValue("@Salary",TextBox4.Text);
cmd.Parameters.AddWithValue("@DeptNo",DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@Gender",RadioButtonList1.SelectedValue);
cmd.Parameters.AddWithValue("@Status",CheckBox1.Checked);
cmd.Parameters.AddWithValue("@Photo", strname);
}
private void LoadData()
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
cmd.CommandText = "Insert into Emp(Eid,Ename,Job,Salary,DeptNo,Gender,Status,Photo)values(@Eid,@Ename,@Job,@Salary,@DeptNo,@Gender,@Status,@Photo)";
ShowData();
LoadData();
}
protected void Button2_Click(object sender, EventArgs e)
{
cmd.CommandText = "Update Emp set Ename=@Ename,Job=@Job,Salary=@Salary,DeptNo=@DeptNo,Gender=@Gender,Status=@Status,Photo=@Photo where Eid=@Eid" +strname;
ShowData();
LoadData();
}
protected void Button3_Click(object sender, EventArgs e)
{
cmd.CommandText = "Delete from Emp where Eid=@Eid";
cmd.Parameters.AddWithValue("@Eid", TextBox1.Text);
LoadData();
}
protected void Button4_Click(object sender, EventArgs e)
{
cmd.CommandText = "Select * from Emp";
LoadData();
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "InsertData");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
No comments:
Post a Comment