JB4375
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
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.DataSou rce = YourDataSource;
this.DropDownList1.DataBin d();
}
}
Using parameters within the SQL statement...Is that for the Sub that handles the drop down, Add SQL, or both?
Thanks in advance.
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.DataSou
this.DropDownList1.DataBin
}
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(By Val sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.DataBound
DropDownList1.Items.Insert (0, New ListItem("Select One", ""))
To this:
Protected Sub DropDownList1_SelectedInde xChanged(B yVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedInde xChanged
If DropDownList1.SelectedInde x = 0 Then
TextBox1.Text = ""
Else
Thanks again!!
Protected Sub DropDownList1_DataBound(By
DropDownList1.Items.Insert
To this:
Protected Sub DropDownList1_SelectedInde
If DropDownList1.SelectedInde
TextBox1.Text = ""
Else
Thanks again!!
ASKER
I appreciate your patience and the extra effort you put in. Thanks again!
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="Dr
<asp:ListItem Text="Select One" Value="" />
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.DropDownList1.DataSou
this.DropDownList1.DataBin
}
}
then get your contacts from the selected index changed event
protected void DropDownList1_SelectedInde
{
// 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.AddWithVa