Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:57:10+00:00 2026-05-23T08:57:10+00:00

I am getting confused with my code and not sure how to implement what

  • 0

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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:57:10+00:00Added an answer on May 23, 2026 at 8:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framwwork and Code First and getting really confused. I have
I am getting a little confused and need some help please. Take these two
Every time that I want to do a Layout, I'm getting a black layout
I am confused about strings (a beginner's problem, I'm afraid): I have one NSMutableArray
I'm having trouble getting jQuery to load inside a file that has been imported
I'm getting confused with the include/exclude jargon, and my actual SVN client doesn't seem
Im trying to get into some basic JavaFX game development and I'm getting confused
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
Im getting frustrated because of OpenDNS and other services (ie: roadrunner) that now always
I'm getting confused re the range of options for development & deploying some simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.