Link to home
Start Free TrialLog in
Avatar of JB4375
JB4375Flag for United States of America

asked on

ASP/VB .Net Text Box Not Populating

I have a SQL Table with the following fields: Dept Name, Dept, and Contacts. On the form, when a person selects the Dept Name it should automatically populate the corresponding contact into a textbox on the form, but it isn't firing. Further, It doesn't appear to be picking up anything because it's the only info that's not going into the table.
ASP Drop Down:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" AutoPostBack="True" DataSourceID="SqlDataSource3"
 DataTextField="DeptName" DataValueField="Dept" > </asp:DropDownList>
 
VB.net:
  Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.DataBound
        DropDownList1.Items.Insert(0, New ListItem("Select One", ""))
 
        intcount = 0
        Dim sConn As Data.SqlClient.SqlConnection
        Dim sComm As Data.SqlClient.SqlCommand
        Dim sAdapt As Data.SqlClient.SqlDataAdapter
 
        sConn = New Data.SqlClient.SqlConnection("server=server1;database=dbase;UID=useID;PWD=pass")
        sComm = New Data.SqlClient.SqlCommand("SELECT Contacts From database where Dept = '" & DropDownList1.SelectedItem.Text & "'", sConn)
        sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)
        sConn.Open()
 
        Dim r As Data.SqlClient.SqlDataReader = sComm.ExecuteReader()
        While r.Read()
            Dim strEmail As String = CStr(r("Contacts"))
            TextBox5.Text = strEmail
        End While
        r.Close()
 
        If DropDownList1.SelectedItem.Text = "Select One" Then
            TextBox5.Text = ""
        End If
 
        Image2.Visible = False
        Image3.Visible = False
        Label10.Visible = False
        Label10.Text = ""
    End Sub
 
    Public Sub Add_SQL()
        Dim sConn As Data.SqlClient.SqlConnection
        Dim sComm As Data.SqlClient.SqlCommand
        Dim sAdapt As Data.SqlClient.SqlDataAdapter
 
        sConn = New Data.SqlClient.SqlConnection("server=server1;database=dbase;UID=useID;PWD=pass")
        sComm = New Data.SqlClient.SqlCommand("INSERT INTO autodatabase
(Contractor, FN, MI, LN, Requestor, Submitted, Contacts) VALUES ('" & DropDownList1.SelectedItem.Text & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & strUser & "','" & strDate & "','" & TextBox5.Text & "')", sConn)
        sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)
        sConn.Open()
        sComm.ExecuteNonQuery()
        sConn.Close()
 
    End Sub
End Class

Open in new window

Avatar of wht1986
wht1986
Flag of United States of America image

You want to fetch the values on select index changed, not on the data bound event

On my page I would include the default item in the markup and bind on page load like:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"
    AutoPostBack="true"
    DataTextField="DeptName" DataValueField="Dept"
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="Select One" Value="" />
</asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.DropDownList1.DataSource = YourDataSource;
        this.DropDownList1.DataBind();
    }
}

then get your contacts from the  selected index changed event

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Now query your database to get the contacts here
}

May I also suggest using parameters in your sql statement

sComm = new SqlCommand("SELECT Contacts From database where Dept=@DEPT", sConn)
sComm.Parameters.AddWithValue("@DEPT", this.DropDownList1.SelectedValue);
Avatar of JB4375

ASKER

OK.... I'm floored.... I'm actually just trying to clean up a page where the last programmer put "Select One" as the top entry of the SQL table. Didn't take it would take so much to get the option in the DDL.
Ok... I've edited the drop down on the ASP. Am I correct in assuming that the rest of the code you've provided is for the VB page?
If so I have a Protected Sub Page Load, listed below, where I pull in the Authorized User in order to submit it to the SQL table. How would the code you've provided below work in relation to that? Assuming it will.
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.DropDownList1.DataSource = YourDataSource;
        this.DropDownList1.DataBind();
    }
}
 
 Using parameters within the SQL statement...Is that for the Sub that handles the drop down, Add SQL, or both?
Thanks in advance.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        strDate = Now
        strUser = (Request.ServerVariables("AUTH_USER"))
 
        If InStr(strUser, "domain\") Then
            strUser = Replace(strUser, "domain\", "")
        End If
        If InStr(strUser, "domain\") Then
            strUser = Replace(strUser, "domain\", "")
        End If
        Label9.Text = "medctr\" & strUser
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

That did it!! I actually only had to make the suggested change at the DDL on the ASP, and the changing the VB protected sub from this:
Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.DataBound
        DropDownList1.Items.Insert(0, New ListItem("Select One", ""))
To this:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedIndex = 0 Then
TextBox1.Text = "" 
Else
Thanks again!!
 
Avatar of JB4375

ASKER

I appreciate your patience and the extra effort you put in. Thanks again!