Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code i want to sort dropdownlist in alphabetically format ,

I am very new beginner of c# & asp.net,
Kindly explain in detail

What I have tried:

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Index : System.Web.UI.Page
{
    JRDSQL jsql = new JRDSQL();
    JRDPopup jpop = new JRDPopup();
    SqlConnection con = new SqlConnection(@"Data Source=xxx.xxx.xxx.xxx,1434; Initial Catalog=apbills_Anay;UId=apbills_Anay;Pwd=Lootbazzar@12345;");
       
    protected void Page_Load(object sender, EventArgs e)
    {
        bool blogin = false;
        if (Session["SID"] != null)
        {
            if (Convert.ToInt32(Session["SID"].ToString()) > 0)
            {
                blogin = true;
            }
        }
        if (blogin == true)
        {
            if (!IsPostBack)
            {

            }
        }
        else
        {
            Response.Redirect("Login.aspx", false);

        }

        //Checking Connection State and opening if closed
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        if (!Page.IsPostBack)
        {

            //Call countries DropDownList on page load event
            BindCountry();
                fillDDSMN();
            
        }
    }
    protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int locationId = Convert.ToInt32(ddlcountry.SelectedValue);          
            //Select all States corresponding to the selected Country
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where locationid= " + locationId, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            ddlstate.DataSource = ds;
            ddlstate.DataTextField = "Dealer_Name";
            ddlstate.DataValueField = "Dealer_Id";
            
            ddlstate.DataBind();
            ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
            //If State is not selected then clear City DropDownList also
            //if (ddlstate.SelectedValue == "0")
            //{
            //    ddlCity.Items.Clear();
            //    ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
            //}
        }
        catch (Exception ex)
        {
            //Printing any exception if occcured.
            Response.Write("Error occured: " + ex.Message.ToString());
        }
        finally
        {
            //Close the connection
            con.Close();
        }      
    }

    
    //protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        int Dealer_Id = Convert.ToInt32(ddlstate.SelectedValue);
    //        //Select all Cities corresponding to the selected State
    //        SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where Dealer_id=" + Dealer_id, con);
    //        DataSet ds = new DataSet();
    //        adp.Fill(ds);           
    //        ddlCity.DataSource = ds;
    //        ddlCity.DataTextField = "City_Name";
    //        ddlCity.DataValueField = "City_id_pk";
    //        ddlCity.DataBind();
    //        ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
    //    }
    //    catch (Exception ex)
    //    {
    //        Response.Write("Error occured : " + ex.Message.ToString());
    //    }
    //    finally
    //    {
    //        con.Close();
    //    }       
    //}

    protected void BindCountry()
    {

        try
        {

            SqlDataAdapter adp = new SqlDataAdapter("select * from Location", con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            ddlcountry.DataSource = ds;
            ddlcountry.DataTextField = "Location";
            ddlcountry.DataValueField = "LocationId";
            
            ddlcountry.DataBind();
            ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
            ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
            //ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        catch (Exception ex)
        {
            Response.Write("Error occured : " + ex.Message.ToString());
        }
        finally
        {
            con.Close();
        }
        
    }
    private void fillDDSMN()
    {
        SqlParameter[] paramdg = {new SqlParameter("@Mode","product")
            };
        jpop.PopupCombo(DDSMN, "sp_Registration", paramdg);
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        try
        {

            SqlParameter[] paramsave = { new SqlParameter("@Mode","sale"),
                                     //  new SqlParameter("@saledate",txtsldate.Text.Trim()),
                                       new SqlParameter("@sale_Invoice",txtslinvoice.Text.Trim()),
                                       new SqlParameter("@Dealer_Name",ddlstate.SelectedValue),
                                       new SqlParameter("@SProduct_name",DDSMN.SelectedValue),
                                        new SqlParameter("@Model_name",DDSMN.SelectedValue),
                                       new SqlParameter("@SProduct_Qty",txtslmdlqty.Text.Trim()),
                                       new SqlParameter("@sale_rate",txtslrate.Text.Trim()),
                                       //new SqlParameter("@Scredit",nmbcrd.Text.Trim()),
                                        new SqlParameter("@Scash",nmbcsh.Text.Trim()),
                                       // new SqlParameter("@Scash",nmbcsh.Text.Trim()),
                                        //new SqlParameter("@Product_cat",ddSMC.SelectedValue), 
                                         new SqlParameter("daily_scash",nmbcsh.Text.Trim()),
                                         //new SqlParameter("Daily_Scredit",nmbcrd.Text.Trim()),
                                   };

            int id = 0;
            id = jsql.execStoredProcudure("sp_sale", paramsave);

            if (id > 0)
            {
                JRDMessage.UserMessage(this, "Data submitted successfully !!!");

            }
        }
        catch (Exception ex)
        {
            JRDMessage.UserMessage(this, ex.Message);
        }
    }




    }
Posted
Updated 11-Jul-17 15:44pm
v3
Comments

The easiest method is to add an Order By clause to your SQL.

Specifically;
C#
SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where locationid = " + locationid + " ORDER BY Dealer_Name", con);

AND
C#
SqlDataAdapter adp = new SqlDataAdapter("select * from Location ORDER BY Location");
 
Share this answer
 
v2
Just add the ORDER BY to your select query. Also, you can try adding a DropDownList that will serve as sort alphabetically in Ascending or Descending order. Just a suggestion :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900