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

时间:2016-01-11 00:07:52

标签: c# asp.net sql-server database

我无法在下方获取代码,将DropDownList设置为正确的值。

它的else语句中的代码对我不起作用,但它也没有抛出任何异常。

DropDownList是从一个存储程序信息的数据库设置的。我用reader1访问的数据库是一个用户信息数据库,它具有从DropDownList注册时选择的字符串值,所以我不能使用索引。有人可以指出我出错的地方吗?

我只想从用户数据库中检索我的文本字符串,从程序数据库中填充DropDownList,然后让数据库显示与该字符串匹配的项目。它的最后一部分,自动选择项目,这就是问题所在。

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;
                                        }

                                    }
                                }
                            }
                        }

1 个答案:

答案 0 :(得分:0)

尝试将您的第一种方法更改为:

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;
    }