Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i select area from dropdown list it should display all attributes from the same table


code:(it shows error)

public void area()
    {
        if (ddlplace.SelectedItem.Value == "yes")
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Close();
            }
            SqlCommand cmd = new SqlCommand("select delivery_charges,tax,shipping from delivery ", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlplace.DataSource = ds;
            ddlplace.DataTextField = "area";
            ddlplace.DataValueField = "area";
            ddlplace.DataBind();
            con.Close();

        }
Posted
Updated 27-Nov-11 21:42pm
v2
Comments
koolprasad2003 28-Nov-11 3:42am    
what is the error ?

whatever error you are getting let it be , first thing that is wrong in your code is you are trying to close already closed connection , change the code for connection opening as:
C#
if (con.State == ConnectionState.Closed)
           {
               con.Open();
           }
 
Share this answer
 
v2
Comments
M.Narmatha 28-Nov-11 1:33am    
even error coming..
srinivas vadepally 28-Nov-11 1:36am    
Narmatha,
what is your error? Can you elaborate?
try this it works

C#
{
        if (!IsPostBack)
        {
            // Declare objects

            // Read the connection string from Web.config
            string connectionString =
            ConfigurationManager.ConnectionStrings[
            ""].ConnectionString;
            // Initialize connection
            conn = new SqlConnection(connectionString);

            SqlCommand = new SqlCommand(
            "select delivery_charges,tax,shipping from delivery", conn);


            try
            {
                // Open the connection
                conn.Open();
                // Execute the command
                reader = SqlCommand.ExecuteReader();
                // Populate the list of area
                ddlplace.DataSource = reader;
                ddlplace.DataValueField = "area";
                ddlplace.DataTextField = "area";
                ddlplace.DataBind();
                // Close the reader
                reader.Close();
            }



            finally
            {
                // Close the connection
                conn.Close();
            }
        }
    }
 
Share this answer
 
v4
Comments
[no name] 28-Nov-11 4:05am    
EDIT: added "pre" tag
public void area()
{
if (ddlplace.SelectedItem.Value == "yes")
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select delivery_charges,tax,shipping from delivery ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlplace.DataSource = dt;
ddlplace.DataTextField = "area";
ddlplace.DataValueField = "area";
ddlplace.DataBind();
con.Close();

}


The area named field not in the table add that and use this code
 
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