Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Second drop down list populates with all 0 for DataValue field

Status
Not open for further replies.

rotaxmax12

Technical User
Mar 22, 2011
5
US
I have a data entry form that you select a primary and secondary contact. The first drop down list populates just fine, but the second drop down list is loading with all 0 (zeros) in the DataValue field. I see this when I View Source on the page.

This is happening when the page is building. No data entry has taken place yet.

VB Code:
'This list populates correctly.
SQLText = "SELECT idsContactID, LTRIM(RTRIM(IsNull(strFirstName,'') + ' ' + IsNull(strLastName,''))) AS ContactName FROM dbo.tblContacts WHERE idnCompanyID=" & Session("CompanyID")
cmd = New SqlCommand(SQLText, BrandedConn)
Dim pcon As SqlDataReader
pcon = cmd.ExecuteReader()

Do
With DesigAppContact
.DataSource = pcon
.DataTextField = "ContactName"
.DataValueField = "idsContactID"
.DataBind()
.Items.Insert(0, New ListItem("Select Primary Contact", 0))
End With
Loop While pcon.Read()
pcon.Close()

'This is populating the drop down with the DataValue = 0
SQLText = "SELECT idsContactID, LTRIM(RTRIM(IsNull(strFirstName,'') + ' ' + IsNull(strLastName,''))) AS ContactName FROM dbo.tblContacts WHERE idnCompanyID=" & Session("CompanyID")
cmd = New SqlCommand(SQLText, BrandedConn)
Dim dccCon As SqlDataReader
dccCon = cmd.ExecuteReader()
Do
With DesigClaimContact
.DataSource = dccCon
.DataTextField = "ContactName"
.DataValueField = "idsContactID"
.DataBind()
.Items.Insert(0, New ListItem("Select Designated Claim Contact", 0))
End With
Loop While dccCon.Read()
dccCon.Close()

HTML/ASP Page code:
<tr>
<td class="styleLeftSide">Designated Application Contact:</td>
<td class="styleRightSide"><asp:DropDownList ID="DesigAppContact" runat="server" DataValueField="idsContactID" DataTextField="ContactName"></asp:DropDownList></td>
</tr>
<tr>
<td class="styleLeftSide">Designated Claims Contact:</td>
<td class="styleRightSide"><asp:DropDownList ID="DesigClaimContact" runat="server" DataValueField="idsContactID" DataTextField="ContactName"></asp:DropDownList></td>
</tr>

Many thanks for any assistance.

JJ
 
Basically you are duplicating code, and possibly have a cut and paste error on the second select statement.
I would get rid of the sqldatareader, it's not buying you much in the way of speed of loading of the page.

I would run the select statement once and return a datatable instead of a datareader. Then just bind the datatable to each ddl.
This would remove the two loops which are unnecessary.

Another couple of suggestions:
1.) Write a stored procedure to do the select. Makes for more maintainable code and is optimize by SQL.
2.) Create a DAL(Data Access Layer).. Basically just a class that calls the SQL. This way you can just pass in the conn string and pass back a reader, dataset, datatable etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top