?
Solved

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

Posted on 2009-02-08
3
Medium Priority
?
555 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:pratikoza
3 Comments
 
LVL 7

Expert Comment

by:lakshmidurga
ID: 23587709
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
 
LVL 12

Accepted Solution

by:
CB_Thirumalai earned 60 total points
ID: 23587717
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
 

Author Closing Comment

by:pratikoza
ID: 31544406
sorry i am new in c# i do't understand ur solution plz give full complete code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question