选择使用String一个SQL数据库的DropDownList值 [英] Select DropDownList value using String from SQL database

查看:129
本文介绍了选择使用String一个SQL数据库的DropDownList值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让我的code低于我的DropDownList设置为正确的值。

其code。在不是为我工作,但其也不抛出任何异常。在其他语句

DropDownList控件从一个数据库,在那里我存储程序信息被设置。我与 reader1 访问数据库是具有从DropDownList的注册选择的字符串值的用户信息数据库,所以我不能使用索引。可能有人请指出我要去哪里错了?

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

 私人数据表loadAdBusinessTypes1()
        {
            。字符串prgInfoConnection = ConfigurationManager.ConnectionStrings [program_infoConnection]的ToString();
            SqlConnection的oSqlConnection =新的SqlConnection(prgInfoConnection);
            的SqlCommand oSqlCommand =新的SqlCommand();
            oSqlCommand.Connection = oSqlConnection;
            oSqlCommand.CommandType = CommandType.StoredProcedure;
            oSqlCommand.CommandText =pGet​​AdBusinessTypes;
            SqlDataAdapter的oSqlDataAdapter =新的SqlDataAdapter();
            oSqlDataAdapter.SelectCommand = oSqlCommand;
            数据表oDataTable1 =新的DataTable(AdBusinessTypes);
            oSqlDataAdapter.Fill(oDataTable1);
            返回oDataTable1;
        }        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(会话[新建] == NULL)
            {
                的Response.Redirect(〜/帐号/的Login.aspx); // ***** CHANGE重定向WEBPAGE *****
            }
            其他
            {
                如果(!Page.IsPostBack)
                {
                    数据表oDataTable = loadAdBusinessTypes1();
                    BusTypeddl.DataSource = oDataTable;
                    BusTypeddl.DataTextField =business_type;
                    BusTypeddl.DataBind();
                }                尝试
                {
                    如果(会话[新建] == NULL)
                    {
                        的Response.Redirect(〜/帐号/的Login.aspx); // ***** CHANGE重定向WEBPAGE *****
                    }
    其他
                        {                            字符串str = Convert.ToString(会话[新建]);
                            字符串声明cmdtext = @选择account_no上,名字,姓氏,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,电话code_1,电话code_2,网站,REGISTRATION_DATE,screens_no,从用户那里EMAIL_1 = @电子邮件USER_PASSWORD;
                            。字符串cpUsersConnection = ConfigurationManager.ConnectionStrings [cp_usersConnection]的ToString();
                            使用(SqlConnection的oSqlConnection =新的SqlConnection(cpUsersConnection))
                            使用(的SqlCommand oSqlCommand =新的SqlCommand(声明cmdtext,oSqlConnection))
                            {
                                oSqlConnection.Open();
                                oSqlCommand.Parameters.Add(@电子邮件,SqlDbType.NVarChar).value的STR =;
                                使用(SqlDataReader的reader1 = oSqlCommand.ExecuteReader())
                                {
                                    如果(reader1.Read())
                                    {                                        。VAR findBusType = reader1 [business_type]的ToString()修剪();
                                        变种的selectedIndex = -1;                                        的for(int i = 0; I< BusTypeddl.Items.Count;我++)
                                        {
                                            如果(BusTypeddl.Items [I]的ToString()== findBusType)
                                            {
                                                的selectedIndex = I;
                                                打破;
                                            }
                                        }
                                        如果(的selectedIndex -1个)
                                        {
                                            BusTypeddl.SelectedIndex =的selectedIndex;
                                        }                                    }
                                }
                            }
                        }


解决方案

试着改变你的第一种方法:

 私人数据表loadAdBusinessTypes1()
    {
        。字符串prgInfoConnection = ConfigurationManager.ConnectionStrings [program_infoConnection]的ToString();
        SqlConnection的oSqlConnection =新的SqlConnection(prgInfoConnection);
        的SqlCommand oSqlCommand =新的SqlCommand(pGet​​AdBusinessTypes,oSqlConnection);
        oSqlCommand.CommandType = CommandType.StoredProcedure;
        DataTable的DT =新的DataTable();
        尝试{
            oSqlConnection.Open();
            SqlDataAdapter的oSqlDataAdapter =新SqlDataAdapter的(oSqlCommand);
            数据表oDataTable1 =新的DataTable(AdBusinessTypes);
            oSqlDataAdapter.Fill(oDataTable1);
            DT = oDataTable1;
        }
        赶上(例外前){
            //你可能会异常在这里工作
        }
        最后{
            oSqlConnection.Close();
        }
        返回DT;
    }

I can't get my code below to set my DropDownList to the correct value.

Its the code in the else statement that is not working for me but its not throwing any exceptions either.

The DropDownList is being set from one database where I store program information. 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. 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. 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;
    }

这篇关于选择使用String一个SQL数据库的DropDownList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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