How to use database using CLASS in asp.net
website -> add new item -> select class ( Apply code )
public class Class1
{
SqlDataReader dr;
SqlCommand cm;
SqlConnection cn;
public string Connection()
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
return cs;
}
public string querry()
{
string k = "select * from Table1";
return k;
}
public string INSERT()
{
string q = "insert into Table1 values";
return q;
}
}
------------------------------------------------------------------------------------------------------------------
create aspx page and apply code
public void LoadGridview()
{
Class1 my = new Class1();
string cs = my.Connection();
cn = new SqlConnection(cs);
cn.Open();
string k= my.querry();
cm = new SqlCommand(k, cn);
dr = cm.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Class1 MY1 = new Class1();
string cs = MY1.Connection();
cn = new SqlConnection(cs);
cn.Open();
string k = MY1.INSERT()+"('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";;
cm = new SqlCommand(k, cn);
cm.ExecuteNonQuery();
Response.Write("Data saved");
}
website -> add new item -> select class ( Apply code )
public class Class1
{
SqlDataReader dr;
SqlCommand cm;
SqlConnection cn;
public string Connection()
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
return cs;
}
public string querry()
{
string k = "select * from Table1";
return k;
}
public string INSERT()
{
string q = "insert into Table1 values";
return q;
}
}
------------------------------------------------------------------------------------------------------------------
create aspx page and apply code
public void LoadGridview()
{
Class1 my = new Class1();
string cs = my.Connection();
cn = new SqlConnection(cs);
cn.Open();
string k= my.querry();
cm = new SqlCommand(k, cn);
dr = cm.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Class1 MY1 = new Class1();
string cs = MY1.Connection();
cn = new SqlConnection(cs);
cn.Open();
string k = MY1.INSERT()+"('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";;
cm = new SqlCommand(k, cn);
cm.ExecuteNonQuery();
Response.Write("Data saved");
}
Nice blog.
ReplyDeleteI've gone through the details that you mentioned in the blog and I believe that the details that you mentioned regarding Asp Dot Net is very fruitful and people should lean from this tutorial.
Thanks for sharing such a great blog.
Asp Dot Net Core