如何在数据库的下拉列表中显示值? [英] How do I show the value in dropdown list from database?

查看:76
本文介绍了如何在数据库的下拉列表中显示值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ConnectionString:



< add name =LifetimeConnectionconnectionString =Data Source =(LocalDb)\ v11.0; AttachDbFilename = | DataDirectory | \Sample.mdf; Integrated Security = True

providerName =System.Data.SqlClient/>



这个是C#代码:

Here is my ConnectionString:

<add name="LifetimeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

This is C# code:

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.SqlClient;
using System.Web.Configuration;
using System.Configuration;

namespace LifeTime
{
    public partial class index : System.Web.UI.Page
    {
        //static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/LifeTime");
        //SqlConnection con = null;
        //string constr = ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString;
        //System.Configuration.Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
        //const string CONSTRINGNAME = "LifetimeConnection";
        //ConnectionStringSettings conString = rootWebConfig.ConnectionStrings.ConnectionStrings[CONSTRINGNAME];
        //SqlConnection con = new SqlConnection(rootWebConfig.ConnectionStrings[CONSTRINGNAME].ConnectionStrings);
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString);
            //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog = Sample;Trusted_Connection=true;");
            //Response.Write(con);
            //SqlCommand cmd = new SqlCommand("Select ID,location_name from dbo.location where flag = 0;",con);
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString);

            try
            {
                Response.Write("Hi Hello");
                con.Open();
                Response.Write("Con is opened");
                SqlCommand cmd = new SqlCommand("Select top 1 * from location;", con);
                dropdownlist.DataSource = cmd.ExecuteReader();
                dropdownlist.DataBind();
                dropdownlist.DataTextField = "location_name";
                dropdownlist.DataValueField = "ID";
                dropdownlist.DataBind();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                con.Close();
            }
        }
    }
}

请建议我一个方法。我会感谢你的。



我尝试过的事情:



我尝试了所有google方式,但没有结果

Please suggest me a way. I will be thankful to you.

What I have tried:

I have tried all google ways, but no result come

推荐答案

嗨会员13209785,



Do你只想显示 Id 以及 location_name ?然后使用Id进行简单连接并显示连接项是最快的解决方案。

Hi Member 13209785,

Do you just want to just display the Id along with location_name? Then a simple concatenation with the Id and displaying the concatenated item is the quickest solution for this.
SqlCommand cmd = new SqlCommand("Select top 1 * from location;", con);

上面一行更改为:

The above line changes to:

SqlCommand cmd = new SqlCommand("Select top 1 *, CONVERT(VARCHAR, ID) + ' ' + location_name AS LocationNameWithId from location", con);

然后将数据源设置为 LocationNameWithId

Then set the datasource to LocationNameWithId:

dropdownlist.DataTextField = "LocationNameWithId";

此外,我没有看到第一个 DataBind()语句的使用。您可以删除它。



但是,您的代码容易受到SQL注入攻击。请仔细阅读并妥善保管。

Also I don't see any use of the first DataBind() statement. You can remove that.

However, your code is vulnerable to SQL injection attacks. Please read about them and take care accordingly.


这篇关于如何在数据库的下拉列表中显示值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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