当我从下拉列表中选择区域时,它应显示同一表中的所有属性. [英] when i select area from dropdown list it should display all attributes from the same table.

查看:74
本文介绍了当我从下拉列表中选择区域时,它应显示同一表中的所有属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从下拉列表中选择区域时,它应该显示同一表中的所有属性


代码:(显示错误)

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();

        }

推荐答案

无论您遇到什么错误,代码中首先出现的错误是您试图关闭已经关闭的连接,更改代码用于连接的打开方式为:
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:
if (con.State == ConnectionState.Closed)
           {
               con.Open();
           }


尝试一下,

try this it works

{
        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();
            }
        }
    }


公共无效区域()
{
如果(ddlplace.SelectedItem.Value ==是")
{
如果(con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd =新的SqlCommand("select delivery_charges,tax,shipping from delivery",con);
SqlDataAdapter da =新的SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlplace.DataSource = dt;
ddlplace.DataTextField =区域";
ddlplace.DataValueField ="area";
ddlplace.DataBind();
con.Close();

}


不在表中的名为field的区域添加并使用此代码
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


这篇关于当我从下拉列表中选择区域时,它应显示同一表中的所有属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆