crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

create common funcion to fill dataset in asp.net
Question Posted on 23 Aug 2011Home >> DotNet >> ASP.NET >> create common funcion to fill dataset in asp.net

// Web.Config File Write Below Code
< connectionStrings >
 < add name="connectionname" connectionString="user id=sa1; pwd=; Initial Catalog=databasename;Data Source=000.00.00.000" providerName="System.Data.SqlClient"/ >
< /connectionStrings >

//And then declare connection string in web.config and use this on our class file
public static string m_strConnection = ConfigurationManager.ConnectionStrings["connectionname"].ToString();

public static SqlConnection GetDataConnection()
{
 SqlConnection sql_cn = new SqlConnection(m_strConnection);
 sql_cn.Open();
 return sql_cn;
}

//different function in class file
///function with no parameter return dataset
public static SqlDataAdapter GetDataAdapter(string sql)
{
 SqlDataAdapter sql_adp;
 SqlCommand sql_cd = new SqlCommand();
 DataSet sql_ds = new DataSet();
 sql_cd.CommandText = sql;
 //sql_cd.CommandType = CommandType.StoredProcedure;
 sql_cd.Connection = GetDataConnection();
 sql_adp = new SqlDataAdapter(sql_cd);
 return sql_adp;
}


///Belowfunction return dataset and accept hashtable as parameter
public static SqlDataAdapter GetDataAdapter(string sql, Hashtable param)
{
  Hashtable parameter = param;
 SqlDataAdapter sql_adp;
 SqlCommand sql_cd = new SqlCommand();
  //string hshKey="";
  DataSet sql_ds = new DataSet();
 sql_cd.CommandText = sql;
 sql_cd.CommandType = CommandType.StoredProcedure;
 sql_cd.Connection = GetDataConnectionIP();
 sql_cd.CommandTimeout = 5000;
 if (!(param == null))
  {
   if ((int)param.Count > 0)
  {
   foreach (string hsh_Key in param.Keys)
   {
    sql_cd.Parameters.Add(new SqlParameter(hsh_Key, parameter[hsh_Key]));
   }
  }
 }
 sql_adp = new SqlDataAdapter(sql_cd);
 sql_adp.Fill(sql_ds);
  if (sql_cd.Connection.State == ConnectionState.Open)
 {
  sql_cd.Connection.Close();
  sql_cd.Dispose();
 }
 return sql_ds.Copy();
}
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

Use of Calendar control in dotnet with code

How to improve Front End UI performance of dotnet web application?

File upload in dotnet

reverse a string

function of garbage collector






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap