简体   繁体   English

从SQL数据库中使用String选择DropDownList值

[英]Select DropDownList value using String from SQL database

I can't get my code below to set my DropDownList to the correct value. 我无法在下面的代码中将DropDownList设置为正确的值。

Its the code in the else statement that is not working for me but its not throwing any exceptions either. else语句中的代码对我不起作用,但也不抛出任何异常。

The DropDownList is being set from one database where I store program information. 我从一个存储程序信息的数据库中设置了DropDownList。 The database I'm accessing with reader1 is a user information database which has the string value selected on registration from the DropDownList, so I cant use the index. 我正在使用reader1访问的数据库是一个用户信息数据库,该数据库具有在注册时从DropDownList中选择的字符串值,因此无法使用索引。 Could someone please point out where I'm going wrong? 有人可以指出我要去哪里了吗?

I just want to retrieve my text string from the user database, populate the DropDownList from the program database, then have the database show the item that matches the string. 我只想从用户数据库中检索我的文本字符串,从程序数据库中填充DropDownList,然后让数据库显示与字符串匹配的项目。 Its this last part, auto selecting the item, thats the problem. 它的最后一部分,自动选择项目,就是问题所在。

private DataTable loadAdBusinessTypes1()
        {
            string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
            SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
            SqlCommand oSqlCommand = new SqlCommand();
            oSqlCommand.Connection = oSqlConnection;
            oSqlCommand.CommandType = CommandType.StoredProcedure;
            oSqlCommand.CommandText = "pGetAdBusinessTypes";
            SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
            oSqlDataAdapter.SelectCommand = oSqlCommand;
            DataTable oDataTable1 = new DataTable("AdBusinessTypes");
            oSqlDataAdapter.Fill(oDataTable1);
            return oDataTable1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["New"] == null)
            {
                Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    DataTable oDataTable = loadAdBusinessTypes1();
                    BusTypeddl.DataSource = oDataTable;
                    BusTypeddl.DataTextField = "business_type";
                    BusTypeddl.DataBind();
                }

                try
                {
                    if (Session["New"] == null)
                    {
                        Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
                    }
    else
                        {

                            string str = Convert.ToString(Session["New"]);
                            string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
                            string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
                            using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
                            using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
                            {
                                oSqlConnection.Open();
                                oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
                                using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
                                {
                                    if (reader1.Read())
                                    {

                                        var findBusType = reader1["business_type"].ToString().Trim();
                                        var selectedIndex = -1;

                                        for (int i = 0; i < BusTypeddl.Items.Count; i++)
                                        {
                                            if (BusTypeddl.Items[i].ToString() == findBusType)
                                            {
                                                selectedIndex = i;
                                                break;
                                            }
                                        }
                                        if (selectedIndex > -1)
                                        {
                                            BusTypeddl.SelectedIndex = selectedIndex;
                                        }

                                    }
                                }
                            }
                        }

Try to change your first method to: 尝试将您的第一种方法更改为:

private DataTable loadAdBusinessTypes1()
    {
        string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
        SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
        SqlCommand oSqlCommand = new SqlCommand("pGetAdBusinessTypes", oSqlConnection);
        oSqlCommand.CommandType = CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        try{
            oSqlConnection.Open();
            SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(oSqlCommand);
            DataTable oDataTable1 = new DataTable("AdBusinessTypes");
            oSqlDataAdapter.Fill(oDataTable1);
            dt = oDataTable1;
        }
        catch(Exception ex){
            //you may work with exceptions here
        }
        finally{
            oSqlConnection.Close();
        }
        return dt;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM