ASPX page
<table>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
Name :
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
<td>
<asp:Button ID="btnBack" runat="server" Text="Back" />
</td>
</tr>
</table>
**************************************************************************
cs file of aspx page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//call class file here
Datatbl objData = new Datatbl();
objData.tblId = "";
objData.tblName = txtName.Text;
DAOtbl objDAO = new DAOtbl();
objDAO.insert(objData);
}
}
*****************************************************************
now add class file Datatbl.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public class Datatbl
{
private string id;
private string name;
public string tblId
{
get
{
return id;
}
set
{
id = value;
}
}
public string tblName
{
get
{
return name;
}
set
{
name = value;
}
}
}
*************************************************************************
Now Establish connection Database.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class Database
{
public static SqlConnection connect()
{
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = "Data Source=.\\SQLExpress;AttachDbFileName=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True;";
return con1;
}
}
*****************************************************************
Logic of get data,delete,update DAOtbl.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class DAOtbl
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataAdapter adp = null;
DataTable dt = null;
string query = "";
public Boolean isDuplicate(string name)
{
query = "SELECT COUNT(*) FROM tbl WHERE Name=@Name";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
int count = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
if (count == 0)
return false;
return true;
}
public Boolean insert(Datatbl obj)
{
query = "INSERT INTO tbl VALUES (@Name)";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", obj.tblName);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Boolean update(Datatbl obj)
{
query = "UPDATE tbl SET Name=@Name WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", obj.tblId);
cmd.Parameters.AddWithValue("@Name", obj.tblName);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Boolean delete(string id)
{
query = "DELETE FROM tbl WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Datatbl getDataById(string id)
{
query = "SELECT * FROM tbl WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", id);
dt = new DataTable();
adp = new SqlDataAdapter(cmd);
con.Open();
adp.Fill(dt);
con.Close();
Datatbl dataObj = null;
if (dt != null && dt.Rows.Count == 1)
{
dataObj = new Datatbl();
dataObj.tblId = dt.Rows[0]["Id"].ToString();
dataObj.tblName = dt.Rows[0]["Name"].ToString();
}
return dataObj;
}
public DataTable getData()
{
query = "SELECT * FROM tbl";
con = Database.connect();
cmd = new SqlCommand(query, con);
dt = new DataTable();
adp = new SqlDataAdapter(cmd);
con.Open();
adp.Fill(dt);
con.Close();
return dt;
}
}
<table>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
Name :
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
<td>
<asp:Button ID="btnBack" runat="server" Text="Back" />
</td>
</tr>
</table>
**************************************************************************
cs file of aspx page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//call class file here
Datatbl objData = new Datatbl();
objData.tblId = "";
objData.tblName = txtName.Text;
DAOtbl objDAO = new DAOtbl();
objDAO.insert(objData);
}
}
*****************************************************************
now add class file Datatbl.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public class Datatbl
{
private string id;
private string name;
public string tblId
{
get
{
return id;
}
set
{
id = value;
}
}
public string tblName
{
get
{
return name;
}
set
{
name = value;
}
}
}
*************************************************************************
Now Establish connection Database.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class Database
{
public static SqlConnection connect()
{
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = "Data Source=.\\SQLExpress;AttachDbFileName=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True;";
return con1;
}
}
*****************************************************************
Logic of get data,delete,update DAOtbl.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class DAOtbl
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataAdapter adp = null;
DataTable dt = null;
string query = "";
public Boolean isDuplicate(string name)
{
query = "SELECT COUNT(*) FROM tbl WHERE Name=@Name";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
int count = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
if (count == 0)
return false;
return true;
}
public Boolean insert(Datatbl obj)
{
query = "INSERT INTO tbl VALUES (@Name)";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", obj.tblName);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Boolean update(Datatbl obj)
{
query = "UPDATE tbl SET Name=@Name WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", obj.tblId);
cmd.Parameters.AddWithValue("@Name", obj.tblName);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Boolean delete(string id)
{
query = "DELETE FROM tbl WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (noOfRowsAffected == 1)
return true;
return false;
}
public Datatbl getDataById(string id)
{
query = "SELECT * FROM tbl WHERE Id=@Id";
con = Database.connect();
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", id);
dt = new DataTable();
adp = new SqlDataAdapter(cmd);
con.Open();
adp.Fill(dt);
con.Close();
Datatbl dataObj = null;
if (dt != null && dt.Rows.Count == 1)
{
dataObj = new Datatbl();
dataObj.tblId = dt.Rows[0]["Id"].ToString();
dataObj.tblName = dt.Rows[0]["Name"].ToString();
}
return dataObj;
}
public DataTable getData()
{
query = "SELECT * FROM tbl";
con = Database.connect();
cmd = new SqlCommand(query, con);
dt = new DataTable();
adp = new SqlDataAdapter(cmd);
con.Open();
adp.Fill(dt);
con.Close();
return dt;
}
}