Showing posts with label How to make generic provider. Show all posts
Showing posts with label How to make generic provider. Show all posts

Thursday, June 27, 2013

How to make generic provider

How to make generic provider




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{



    //This Function is to Create Connection
    public static DbConnection CreateConnection(string provider, string connection)
    {
        DbConnection con = null;
        if (con == null)
        {
            try
            {
                DbProviderFactory dp = DbProviderFactories.GetFactory(provider);
                con = dp.CreateConnection();
                con.ConnectionString = connection;
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.Message);
            }
        }
        return con;
    }



    //This Function gets all DataProvider name and information
    public static DataTable GetAllProvider()
    {
        DataTable dt = DbProviderFactories.GetFactoryClasses();
        return dt;
    }

    public static string GetConnectionString(string userProviderName)
    {
        string retreive = null;

        try
        {

       

        ConnectionStringSettingsCollection c = ConfigurationManager.ConnectionStrings;
        if (c != null)
        {
            foreach (ConnectionStringSettings cs in c)
            {
                if (cs.ProviderName == userProviderName)
                {
                   retreive=  cs.ConnectionString;
                   // retreive = cs.ProviderName;
                    break;
                }
            }
        }
        return retreive;

        }
        catch (Exception)
        {
            throw;
           
           
        }
       
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = GetAllProvider();

        for (int i = 0; i < dt.Rows.Count; i++)
            DropDownList1.Items.Add(dt.Rows[i][2].ToString());
        GridView1.DataSource = GetAllProvider();
        GridView1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string _connctionstring = DropDownList1.SelectedItem.Text;
        string s = null;
        if (DropDownList1.SelectedIndex == 0)
        {
             s=GetConnectionString(_connctionstring);
             Response.Write(s);

        }
        else if (DropDownList1.SelectedIndex == 1)
        {
            s = GetConnectionString(_connctionstring);
            Response.Write(s);
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            s = GetConnectionString(_connctionstring);
            Response.Write(s);
        }
        else if (DropDownList1.SelectedIndex == 3)
        {
            s = GetConnectionString(_connctionstring);
            Response.Write(s);
        }
    }
}




change in web.config


<connectionStrings>
<clear/>  "This Tag is very important else it will not work"
<add name="Sql" connectionString="data source=72.233.78.98;uid=sa; pwd=xxxxxxxx;database=PG_SystemDB" providerName="System.Data.SqlClient"/>
<add name="Odbc" connectionString="data source=72.233.78.98;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.Odbc"/>
<add name="Oledb" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.Oledb"/>
<add name="OracleClient" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.OracleClient"/>
</connectionStrings>

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...