Hi @Simflex ,
You need to get the value of dr("Position_title").ToString(), then the SQL statement in cmd1 also needs to link the two tables to get Position_title.
According to your description, there should be no Position_title field in the tblLogin table.
Dim cmd1 As New SqlCommand("SELECT distinct * FROM Positions p LEFT JOIN tblLogin l ON p.positionId = l.positionId where userid=@userid and EMPLMT_STA_CD = 'A' ", myConnection)
Then pass dr("Position_title").ToString() to PositionList.SelectedItem.Text.
PositionList.SelectedItem.Text = dr("Position_title").ToString()
Below is my test based on the code you provided.
<div>
Precinct:<asp:Label ID="Label5" runat="server" ForeColor="#CC0000" Text="*"></asp:Label>
<div class="input text" style="white-space:nowrap">
<asp:DropDownList ID="PositionList" runat="server" AppendDataBoundItems="True" Width="100px" AutoPostBack="True" Enabled="True">
<asp:ListItem value="0" Selected="True"> </asp:ListItem>
</asp:DropDownList>
</div>
<asp:Label ID="NameTB" runat="server" ></asp:Label>
<asp:Label ID="AddressTB" runat="server" ></asp:Label>
<asp:Label ID="PhoneTB" runat="server" ></asp:Label>
<asp:Label ID="EmailTB" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim cmd2 As New SqlCommand("Select distinct p.PositionId, p.Position_title from Positions p left join tblLogin l on p.positionId = l.positionId", New SqlConnection(ConfigurationManager.ConnectionStrings("DBCS").ConnectionString))
cmd2.Connection.Open()
Dim ddlPValues As SqlDataReader
ddlPValues = cmd2.ExecuteReader()
PositionList.DataSource = ddlPValues
PositionList.DataValueField = "PositionId"
PositionList.DataTextField = "Position_title"
PositionList.DataBind()
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim myConnectionString As [String] = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Dim myConnection As New SqlConnection(myConnectionString)
Try
myConnection.Open()
Session("UserId") = 2
Dim cmd1 As New SqlCommand("SELECT distinct * FROM Positions p LEFT JOIN tblLogin l ON p.positionId = l.positionId where userid=@userid and EMPLMT_STA_CD = 'A' ", myConnection)
cmd1.Parameters.AddWithValue("@userid", Session("UserId"))
Dim dr As SqlDataReader = cmd1.ExecuteReader()
If dr.Read() Then
NameTB.Text = dr("fullName").ToString()
AddressTB.Text = dr("Address").ToString()
PhoneTB.Text = dr("Phone_nbr").ToString()
EmailTB.Text = dr("Email").ToString()
PositionList.SelectedItem.Text = dr("Position_title").ToString()
End If
Catch ex As SqlException
Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + ex.Message + "')</SCRIPT>")
Finally
myConnection.Close()
End Try
End Sub
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.