i want comman function like sqldataadapter,dataset that use in class file and also how to access that function from webpage?plz help me.

hi all expert,
i have a connection in web.config file,i am use this connection in webpage,but now i want to use sqldataadapter,dataset for fill data,so currently i have to write all dataadapter,dataset in webpage but i want to write only one time sqldataadapter,dataset and use in different webpage so how can i put in class file?
and how to use in webpage?(Asp.net,c#)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class adminstudreg : System.Web.UI.Page
{
    
    SqlConnection sqa = new SqlConnection();
    SqlDataAdapter dap;
    DataSet ds = new DataSet();
    String t;
    protected void Page_Load(object sender, EventArgs e)
    {
        sqa.ConnectionString = ConfigurationManager.ConnectionStrings["eagle"].ConnectionString;
        sqa.Open();
 
       // btnsubmit.Attributes.Add("onclick", "return validate()");
        for (int y = 2007; y <System.DateTime.Now.Year +1; y++)
        {
            drpyear.Items.Add(y.ToString());
        }
        
       
    }
    
    protected void btnsubmit_Click1(object sender, EventArgs e)
    {
        
        string s = "select * from StudMst";
        dap = new SqlDataAdapter(s, sqa);
        dap.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            int r = 1;
            t = "insert into StudMst values(" + r + ",'" + txtpassword.Text.Replace("'", "''").Trim() + "','" + txtsurname.Text.Replace("'", "''").Trim() + "','" + txtname.Text.Replace("'", "''").Trim() + "','" + txtfather.Text.Replace("'", "''").Trim() + "','" + txtgrandfather.Text.Replace("'", "''").Trim() + "','" + txtmother.Text.Replace("'", "''").Trim() + "','" + txtaddress.Text.Replace("'", "''").Trim() + "','" + txtcity.Text.Replace("'", "''").Trim() + "','" + txtpin.Text.Replace("'", "''").Trim() + "','" + txtphone.Text.Replace("'", "''").Trim() + "','" + txtmob.Text.Replace("'", "''").Trim() + "','" + txtemail.Text.Replace("'", "''").Trim() + "','" + drpgen.SelectedValue + "','" + txtdob.Text.Replace("'", "''").Trim() + "','" + txtpresent.Text.Replace("'", "''").Trim() + "'," + drpyear.SelectedValue + ",'" + drpmonth.SelectedItem + "','" + txtstandard.Text.Replace("'", "''").Trim() + "','" + txtstream.Text.Replace("'", "''").Trim() + "','" + txtphoto.Text.Replace("'", "''").Trim() + "','" + drpexter.SelectedItem + "')";
            SqlCommand cmd = new SqlCommand(t, sqa);
            cmd.ExecuteNonQuery();
 
        }
 
        DataSet ds1 = new DataSet();
        string w = "select max(StudId)+1 from StudMst";
        SqlDataAdapter dap1 = new SqlDataAdapter(w, sqa);
        dap1.Fill(ds1);
        int z = Convert.ToInt32(ds1.Tables[0].Rows[0].ItemArray[0].ToString());
        t = "insert into StudMst values(" + z + ",'" + txtpassword.Text.Replace("'", "''").Trim() + "','" + txtsurname.Text.Replace("'", "''").Trim() + "','" + txtname.Text.Replace("'", "''").Trim() + "','" + txtfather.Text.Replace("'", "''").Trim() + "','" + txtgrandfather.Text.Replace("'", "''").Trim() + "','" + txtmother.Text.Replace("'", "''").Trim() + "','" + txtaddress.Text.Replace("'", "''").Trim() + "','" + txtcity.Text.Replace("'", "''").Trim() + "','" + txtpin.Text.Replace("'", "''").Trim() + "','" + txtphone.Text.Replace("'", "''").Trim() + "','" + txtmob.Text.Replace("'", "''").Trim() + "','" + txtemail.Text.Replace("'", "''").Trim() + "','" + drpgen.SelectedValue + "','" + txtdob.Text.Replace("'", "''").Trim() + "','" + txtpresent.Text.Replace("'", "''").Trim() + "'," + drpyear.SelectedValue + ",'" + drpmonth.SelectedItem + "','" + txtstandard.Text.Replace("'", "''").Trim() + "','" + txtstream.Text.Replace("'", "''").Trim() + "','" + txtphoto.Text.Replace("'", "''").Trim() + "','" + drpexter.SelectedItem + "')";
        SqlCommand cmd1 = new SqlCommand(t, sqa);
        cmd1.ExecuteNonQuery();
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/adminstudmanipulate.aspx");
    }
}

Open in new window

pratikozaAsked:
Who is Participating?
 
CB_ThirumalaiConnect With a Mentor Commented:
Create a class file for the web app.  That will automatically store in the App_Code folder.  Open the file and write a public static that will return the dataset as,
public static DataSet GetData(string query){
  // use the connection string and get the same code here that you have written in the button click to fill the dataset and return it as
  return ds;
}
this can be accessed throughout your app.
0
 
lakshmidurgaCommented:
U can add a class file to ur project and write this function over there

Ex:DataAccess.vb
For Dataset
function Filldataset(byval query as string) as dataset
Dim sqa as new SqlConnection(ConfigurationManager.ConnectionStrings["eagle"].ConnectionString)
        sqa.Open();
dim Sqlcmd as new sqlcommand(query,sqa)
   Dim dap as new  SqlDataAdapter (cmd)
   Dim ds as new dataset
dap.fill(ds)
  sqa.close()
return ds
end function

Accessing it from webpage
create an instance of that class in ur webpage
Dim dac as new dataaccess
then u can use that method in the class by using the dac.function name
ex:Binding data to datagrid
datagrid.datasource=dac.filldataset("select * from employees")
datagrid.databind().
0
 
pratikozaAuthor Commented:
sorry i am new in c# i do't understand ur solution plz give full complete code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.