P ) Database Handling Using Class
* Database Handling Using Class *
- All files inside the App_Code folder
- Class Default property is Private.
- Function Properties Return Value
- Void is called No Return
Q ) How to create class ?
Step 1 :- solution explore -- route -- website -- add new item -- Class(My class) -- yes --ok.
Step 2 :- All delete inside public class code public class { }
* How to create Class Page video *
* Database Handling Using Class Concept video *
* Select Query in class
CLASS CODE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for MySelect
/// </summary>
public class MySelect
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
public string city1;
public string display(string n)
{
string path = ConfigurationManager.AppSettings["db1"];
cn = new SqlConnection(path);
cn.Open();
string k = "select * from emp where name=@name1";
cm=new SqlCommand(k, cn);
cm.Parameters.AddWithValue("name1", n);
dr = cm.ExecuteReader();
if(dr.Read())
{
city1 = dr["city"].ToString();
}
dr.Close();
return k;
}
}
SOURCE CODE :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Q_ClassSelect.aspx.cs" Inherits="Q_ClassSelect" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
id
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<br />
enter name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Find" Width="76px" />
<br />
enter city
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
</div>
</form>
</body>
</html>
CODE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Q_ClassSelect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
MySelect m1 = new MySelect();
string data1 = m1.display(TextBox1.Text);
TextBox2.Text = m1.city1;
}
}
NEW PROGRAME
CLASS CODE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// Summary description for classy
/// </summary>
public class classy
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
public string insertdata(string name2, string city2)
{
string path = @"Data Source=.\SQLEXPRESS;Initial Catalog=vnit;Integrated Security=True";
cn = new SqlConnection(path);
cn.Open();
string k = "insert into classy(name,city)values(@name1,@city1)";
cm = new SqlCommand(k, cn);
cm.Parameters.AddWithValue("name1", name2);
cm.Parameters.AddWithValue("city1", city2);
cm.ExecuteNonQuery();
return k;
}
public string deletedata(string name2)
{
string path = @"Data Source=.\SQLEXPRESS;Initial Catalog=vnit;Integrated Security=True";
cn = new SqlConnection(path);
cn.Open();
string k2 = "delete from classy where name=@name1";
cm = new SqlCommand(k2, cn);
cm.Parameters.AddWithValue("name1", name2);
cm.ExecuteNonQuery();
return k2;
}
}
SOURCE CODE :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Q_classy.aspx.cs" Inherits="Q_classy" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
city
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="submit" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="delete" />
</div>
</form>
</body>
</html>
CODE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Q_classy : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
classy c1 = new classy();
c1.insertdata(TextBox1.Text, TextBox2.Text);
Response.Write("data inserted succesfully");
}
protected void Button2_Click(object sender, EventArgs e)
{
classy c2 = new classy();
c2.deletedata(TextBox1.Text);
Response.Write("data deleted succesfully");
}
}
Comments
Post a Comment