简体   繁体   中英

c# asp.net dropdown list select value

I am getting confused with my code and not sure how to implement what I want. I have two sql tables one that has OfficeID and matching OfficeName and another one that contains user. I have a page that allows a person to edit information about the person. When the page is loaded it supposed to select from the drop down list the current OfficeName of a person whose information is being edited. Thus I have this:

This is probably extremely inefficient and confusing for my level of knowledge of C# and SQL, but none the less I am determined to learn how to do it. What I have currently is Before the creation of the drop down list I get the users Id, then select from the database his corresponding officeID, then while creating the drop down list I check for the OfficeID to correspond to the ones from the other table. If it found the match it will set it as the selected value for the drop down list.

am I on the right track? I need to figure out how to compare SESLoginID = loginID before I convert loginID before hand. Any help?

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Functions;
using HelloApp;

public partial class UserUpdate : Page
{
    private Int32 loginID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        loginID = Convert.ToInt32(Request.QueryString["SESLoginID"]);

       if (!Page.IsPostBack)
        {
            BindBusinessUnitDDL();
        }

    }

    protected void BindBusinessUnitDDL()
    {
        SqlConnection conn;
        string sql;
        SqlCommand cmd;
        int error;
        conn = Database.DBConnect(out error);

        sql = String.Format("SELECT OfficeID FROM SESLogin WHERE SESLoginID = loginID");
        cmd = new SqlCommand(sql, conn);
        SqlDataReader rdrr = cmd.ExecuteReader();
        ListItem office = new ListItem();
        office.Value = Convert.ToString(rdrr.GetInt32(0));
        Database.DBClose(conn);   

        sql = String.Format(
            "SELECT OfficeID, OfficeName FROM Office");
        cmd = new SqlCommand(sql, conn);
        SqlDataReader rdr = cmd.ExecuteReader();

        DropDownList ddlBusinessUnit = (DropDownList)(this.LoginFormView.FindControl("ddlBusinessUnit"));

        while (rdr.Read())
        {
            ListItem myItem = new ListItem();
            myItem.Value = Convert.ToString(rdr.GetInt32(0));
            myItem.Text = rdr.GetString(1);
            ddlBusinessUnit.Items.Add(myItem);
            if(office.Value == myItem.Value){
                ddlBusinessUnit.SelectedValue = myItem.Text;
            }
        }
        Database.DBClose(conn);        

        ddlBusinessUnit.DataBind();
        PageUser myUser = new PageUser();
    }

A different version of the code where there exists a procedure to return OfficeName using an LoginID. Doesnt work either gives an error: System.Data.SqlClient.SqlException: Conversion failed when converting the nvarchar value ' SELECT [OfficeName] FROM sesuser.SESLogin INNER JOIN sesuser.Office ON sesuser.Office.OfficeID = sesuser.SESLogin.OfficeID WHERE SESLoginID LIKE '287'' to data type int.

public partial class UserUpdate : Page
{
    private Int32 loginID = 0;
    private String loginIDE = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        loginIDE = Request.QueryString["SESLoginID"];
        loginID = Convert.ToInt32(Request.QueryString["SESLoginID"]);

       if (!Page.IsPostBack)
        {
            BindBusinessUnitDDL();
        }

    }

    protected void BindBusinessUnitDDL()
        {
            SqlConnection connec = null;
            SqlCommand cmd = null;
            string sqls = "";
            int errNum = 0;
            connec = Database.DBConnect(out errNum);
            if (errNum != 0)
                throw new Exception("Database Connection Error.");

            sqls = "Login_GetOffice";
            cmd = new SqlCommand(sqls, connec);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@userID", loginIDE);
            string office = (string)cmd.ExecuteScalar();

        SqlConnection conn;
        string sql;
        int error;
        conn = Database.DBConnect(out error);

        sql = String.Format(
            "SELECT OfficeID, OfficeName FROM Office");
        cmd = new SqlCommand(sql, conn);
        SqlDataReader rdr = cmd.ExecuteReader();

        DropDownList ddlBusinessUnit = (DropDownList)(this.LoginFormView.FindControl("ddlBusinessUnit"));

        while (rdr.Read())
        {
            ListItem myItem = new ListItem();
            myItem.Value = Convert.ToString(rdr.GetInt32(0));
            myItem.Text = rdr.GetString(1);
            ddlBusinessUnit.Items.Add(myItem);
            if(office == myItem.Text){
                myItem.Selected = true;
            }
        }
        Database.DBClose(conn);        

        ddlBusinessUnit.DataBind();
        PageUser myUser = new PageUser();
    }

You can assign a DataSource and Bind the results you get from the query say via a DataTable.

Set the DataTextField and DataValueField

Then you can say something like ddl.Items.FindByText("requiredloginid").Selected = true after the Data is bound to the dropdown.

Why are you using

ddlBusinessUnit.DataBind();?

You are binding any data source to the dropdownlist.

Can you specify on which line you are getting error?

Thanks Ashwani

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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