DropDownList Fill and Filter with help of single class in C# asp.net

DropDownList Fill and Filter with help of single class in C# asp.net










web config-


<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="server=sqlexpress;database=i1819;User Id=sa;password=@12345" providerName="System.Data.SqlClient" />

  </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>
----connection

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient ;
using System.Configuration;


/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{

    public static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    public SqlConnection sqlConn = new SqlConnection(connectionString);


}
---------------------------------

drop down class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public class CommonUI
{
    public CommonUI()
    {
    }
    public void BindContrydropdown(DropDownList dropDownList, string DataTextField, string DataValueField, string sqlQuery)
    {
        string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand(sqlQuery, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        dropDownList.DataSource = ds;
        dropDownList.DataTextField = DataTextField;
        dropDownList.DataValueField = DataValueField;
        dropDownList.DataBind();
        dropDownList.Items.Insert(0, new ListItem("--Select--", "0"));
    }



    public void BindDataGrid(GridView  dataGridView,string sqlQuery)
    {
        string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand(sqlQuery, conn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        dataGridView.DataSource = ds;
        dataGridView.DataBind();
        dataGridView.Rows.Count.ToString();
    



    }    

}
-------------------------\\

DataTable

create table [CountryMaster] (
  [ID] int IDENTITY(1,1) NOT NULL ,
  [Name] nvarchar(50)  NULL ,
  [CountryCode] varchar(5)  NULL ,
  PRIMARY KEY (
   [ID]
  )
)

---
create table [state] (
  [ID] int IDENTITY(1,1) NOT NULL ,
  [STATE_NAME] varchar(255)  NULL DEFAULT (''),
  [STATE_CODE] varchar(255)  NOT NULL DEFAULT (''),
  [EXPORTYN] int  NOT NULL DEFAULT ('0'),
  [VEHICLE_CODE] varchar(255)  NOT NULL DEFAULT (''),
  [COUNTRY_ID] int  NOT NULL DEFAULT ((0)),
  PRIMARY KEY (
   [ID]
  )
)
--

Thanks

Post a Comment

Previous Post Next Post

Design a beautiful Login/Signup (with google map) in Flutter | Flutter UI | VS Code | iOS Android