I have a page that inserts information to multiple tables. This part is working fine. What I want to add is when a user adds a new company not only is it inserting to the db but it is also posting the results on a separate page. For instance it will fill in some of the fields of a new form that will be submitted to create a new ticket. I'm not sure how to this part.

This is the code I have for currently:

Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Public Class DisAddCo
    Inherits System.Web.UI.Page
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        ' Retrieve controls
        Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
        Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
        Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
        Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
        Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
        Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
        Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
        Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
        Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)

        If CompanyTextBox Is Nothing Then Return
        If FirstNameTextBox Is Nothing Then Return
        If LastNameTextBox Is Nothing Then Return
        If Address1TextBox Is Nothing Then Return
        If CityTextBox Is Nothing Then Return
        If StateTextBox Is Nothing Then Return
        If ZipTextBox Is Nothing Then Return
        If PhoneTextBox Is Nothing Then Return

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)

        Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
        cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
        cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
        cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
        cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)

        Try
            conn.Open()

            Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())

            cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
            cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)

            cmd.ExecuteScalar()

            cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
            cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
            cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)

            cmd.ExecuteScalar()
        Finally
            If conn.State = System.Data.ConnectionState.Open Then conn.Close()
        End Try

        'Response.Redirect("default.aspx")

    End Sub

End Class

I'm a bit confused on this part, but are you trying to insert the new information, then redirect the page to a new ticket page, where most of the data is already loaded into the fields?

The info submitted on this page is needed before a new service ticket is created. So, once this has been inserted in the database I'd like it to automatically go to the insert new ticket form and the user will fill out the rest of the necessary info.

Okay, then where you have the response redirect at the end of your code, put this:

Response.Redirect("default.aspx?id=" & cusID)

Replace default.aspx with whatever page it is going to. Then on that following page have something like this:

Dim id As String = Trim(Request.QueryString("id"))

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1")
Dim cmd As New SqlCommand("SELECT * FROM Customers WHERE cusID=@cusID", conn)
cmd.Parameters.AddWithValue("@cusID", id)
Try
  conn.Open()
  Dim dtrReader As SqlDataReader = cmd.ExecuteReader()
  if dtrReader.HasRows then
    'found record with that id
    while dtrReader.Read()
      'set your textboxes here
      tbCompanyname.Text = dtrReader("Company")
      tbAddress1.Text = dtrReader("address1")
      'this is sample code. follow the pattern
    end while
  else
    'no customer found for that id. redirect.
    response.redirect("default.aspx")
  end if
  dtrReader.Close()
  conn.Close()
Catch ex As SqlException
  response.write(ex)
End Try

Should this go in a new protected sub? BTW, this is on the same page with the drop downs we've been working on.

Ok good to know, let me see if I have this straight. You will have a page where they will add a company or become a customer, then it will lead to the dropdownlist page where the information should already be populated. Right?

Here is the structure:
Company DDL
Contact DDL
Add Customer/Company Link: (takes you to new page to fill out form)

Dispatch form that is populated by ddls or add Customer Page
This is the one that include the equipment ddl

Then after you fill out the Addcustomer/company form, it redirects you back to that page, where the information is pre-filled out with the information submitted on the form, right?

Ok. Then so on this page with the form, once you insert the information, redirect to the page back there with a querystring.

Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)

Then on that page, (copied code from before) do this under page_load AFTER you populated the dropdownlists:

Dim cusID As String = Trim(Request.QueryString("cusID"))
Dim conID As String = Trim(Request.QueryString("conID"))
DropDownList1.Items.FindByValue(cusID).Selected = True
Dim eventarg As New EventArgs
Call DropDownList1_SelectedIndexChanged(DropDownList1, eventarg.Empty)
DropDownList2.Items.FindByValue(conID).Selected = True
eventarg = New EventArgs
Call DropDownList2_SelectedIndexChanged(DropDownList2, eventarg.Empty)

You also need to change the INSERT to:

Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Public Class DisAddCo
    Inherits System.Web.UI.Page
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        ' Retrieve controls
        Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
        Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
        Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
        Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
        Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
        Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
        Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
        Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
        Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)

        If CompanyTextBox Is Nothing Then Return
        If FirstNameTextBox Is Nothing Then Return
        If LastNameTextBox Is Nothing Then Return
        If Address1TextBox Is Nothing Then Return
        If CityTextBox Is Nothing Then Return
        If StateTextBox Is Nothing Then Return
        If ZipTextBox Is Nothing Then Return
        If PhoneTextBox Is Nothing Then Return

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)

        Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
        cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
        cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
        cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
        cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)

        Try
            conn.Open()

            Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())

            cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
            cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)

            Dim conID As Integer = Conver.ToInt32(cmd.ExecuteScalar())

            cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
            cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
            cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)

            cmd.ExecuteNonQuery()
        Finally
            If conn.State = System.Data.ConnectionState.Open Then conn.Close()
        End Try

        Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)

    End Sub

End Class

Something like that should work.

This sets the page_load to call the selectedindexchanged events so you do not have to redo code.

Does this:

Dim cusID As String = Trim(Request.QueryString("cusID"))
Dim conID As String = Trim(Request.QueryString("conID"))
DropDownList1.Items.FindByValue(cusID).Selected = True
Dim eventarg As New EventArgs
Call DropDownList1_SelectedIndexChanged(DropDownList1, eventarg.Empty)
DropDownList2.Items.FindByValue(conID).Selected = True
eventarg = New EventArgs
Call DropDownList2_SelectedIndexChanged(DropDownList2, eventarg.Empty)

go on the same code page as the redirect or does it go on the code page with the ddls?

Aspx code:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="AddCall.aspx.vb" Inherits="AddCall" title="Hackworth Reprographics Service Express - Add Call" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style5
        {
            font-family: Verdana;
            font-size: 12px;
            color: #000000;
            text-align: left;
            font-weight: bold;
        }
        .style6
        {
            height: 20px;
        }
    .style7
    {
        }
        .style8
        {}
        .style9
        {
            width: 26px;
        }
        .style10
        {
            width: 79px;
        }
        .style11
        {
            width: 134px;
        }
        .style12
        {
            width: 50px;
        }
        .style13
        {
            width: 40px;
        }
        .style14
        {
            width: 149px;
        }
        .style15
        {
            width: 86px;
        }
        .style16
        {
            width: 86px;
            height: 22px;
        }
        .style17
        {
            width: 149px;
            height: 22px;
        }
        .style18
        {
            width: 40px;
            height: 22px;
        }
        .style19
        {
            width: 50px;
            height: 22px;
        }
        .style20
        {
            width: 26px;
            height: 22px;
        }
        .style21
        {
            width: 134px;
            height: 22px;
        }
        .style22
        {
            width: 79px;
            height: 22px;
        }
        .style23
        {
            height: 22px;
        }
    </style>
    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table width="100%" cellpadding="0" cellspacing="0" class="topblack">
            <tr>
                <td width="177px" class="rightborder" bgcolor="#CCCCCC" valign="top">
                    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" 
                        ImageSet="Arrows" style="text-align: left">
                        <ParentNodeStyle Font-Bold="False" />
                        <HoverNodeStyle Font-Underline="True" ForeColor="Maroon" />
                        <SelectedNodeStyle Font-Underline="True" ForeColor="Maroon" 
                            HorizontalPadding="0px" VerticalPadding="0px" />
                        <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" 
                            HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
                    </asp:TreeView>
                    <br />
                </td>
                <td valign="top">
                   
                    <table cellspacing="2" cellpadding="0" width="100%">
                        <tr>
                            <td>
                                <div align="left"><asp:SiteMapPath ID="SiteMapPath1" runat="server" PathSeparator=" | ">
                                    <CurrentNodeStyle CssClass="header" Font-Underline="False" ForeColor="Black" />
                                    <NodeStyle CssClass="header" ForeColor="#990000" />
                                    <RootNodeStyle CssClass="header" ForeColor="Maroon" />
                                </asp:SiteMapPath></div>
                            </td>
                            <td>
                                <div align="right">    <asp:LoginStatus ID="LoginStatus1" runat="server" CssClass="maintext" />
                            </div></td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align: left">
                                <table align="center" cellpadding="0" class="style2" style="width: 99%">
                                    <tr>
                                        <td>
                                            <table cellpadding="0" class="style2">
                                                <tr>
                                                    <td>
                                                        &nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <span class="style5">Select a Company:</span>
                                                        <asp:DropDownList 
                                                            ID="DropDownList1" 
                                                            runat="server" 
                                                            DataSourceID="SqlDataSource1" 
                                                            DataTextField="Company" 
                                                            DataValueField="cusID" 
                                                            CssClass="maintext" 
                                                            ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True">
                                                            <asp:ListItem Value="">Select One</asp:ListItem>
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td
                                                        <span class="maintext"><b>Select a Contact:</b> &nbsp;</span>&nbsp;
                                                        <asp:DropDownList 
                                                            ID="DropDownList2" 
                                                            runat="server" 
                                                            DataSourceID="SqlDataSource2" 
                                                            DataTextField="contID" 
                                                            DataValueField="cusID" 
                                                            CssClass="maintext" 
                                                            ForeColor="Maroon" AutoPostBack="True" AppendDataBoundItems="True">
                                                            <asp:ListItem Value="">Select One</asp:ListItem>
                                                        </asp:DropDownList>
                                                        <asp:Label ID="labelSelection" 
            runat="server" style="z-index: 102; left: 368px; position: absolute; top: 144px">
        </asp:Label>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style6">
                                                        <asp:HyperLink 
                                                            ID="AddCompany" 
                                                            runat="server" 
                                                            CssClass="maintext" 
                                                            Font-Bold="True" 
                                                            ForeColor="Maroon" 
                                                            NavigateUrl="~/ServiceExpress/DisAddCo.aspx">Add New Company?</asp:HyperLink>
                                                        <br />
                                                        <br />
                                                        <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" 
                                                            DataSourceID="SqlDataSource4">
                                                            <InsertItemTemplate>
                                                                <table cellpadding="0" 
    class="style2">
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            Company:</td>
                                                                        <td colspan="5" style="margin-left: 40px">
                                                                            <asp:TextBox ID="Company" runat="server" Width="250px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            Phone:</td>
                                                                        <td>
                                                                            <asp:TextBox ID="Phone" runat="server" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            First Name:</td>
                                                                        <td colspan="5">
                                                                            <asp:TextBox ID="FirstName" runat="server" Width="200px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="style10">
                                                                            &nbsp;</td>
                                                                        <td>
                                                                            &nbsp;</td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            Last Name:
                                                                        </td>
                                                                        <td colspan="5">
                                                                            <asp:TextBox ID="LastName" runat="server" style="margin-bottom: 0px" 
                                                                                Width="200px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            Technician:</td>
                                                                        <td>
                                                                            <asp:DropDownList ID="DropDownListTech" runat="server" CssClass="maintext" 
                                                                        DataSourceID="SqlDataSource3" DataTextField="techid" 
                                                                        DataValueField="UserID" AppendDataBoundItems="True">
                                                                        <asp:ListItem Value="">Unassigned</asp:ListItem>
                                                                         </asp:DropDownList>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            Address:</td>
                                                                        <td colspan="5">
                                                                            <asp:TextBox ID="Address1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            Priority:</td>
                                                                        <td>
                                                                            <asp:DropDownList ID="DropDownListPriority" runat="server" CssClass="maintext">
                                                                                <asp:ListItem Selected="True">Select One</asp:ListItem>
                                                                                <asp:ListItem>Red (High)</asp:ListItem>
                                                                                <asp:ListItem>Yellow</asp:ListItem>
                                                                                <asp:ListItem>Blue</asp:ListItem>
                                                                                <asp:ListItem>Green (Low)</asp:ListItem>
                                                                            </asp:DropDownList>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="style15">
                                                                            &nbsp;</td>
                                                                        <td colspan="5">
                                                                            <asp:TextBox ID="Address2" runat="server" Width="200px" 
                                                                                style="margin-bottom: 0px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            Billing:</td>
                                                                        <td>
                                                                            <asp:DropDownList ID="DropDownListBill" runat="server" CssClass="maintext">
                                                                                <asp:ListItem Selected="True">Select One</asp:ListItem>
                                                                                <asp:ListItem>Contract</asp:ListItem>
                                                                                <asp:ListItem>Time/Materials</asp:ListItem>
                                                                            </asp:DropDownList>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            City:</td>
                                                                        <td class="style14">
                                                                            <asp:TextBox ID="City" runat="server" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            State:</td>
                                                                        <td class="style12">
                                                                            <asp:TextBox ID="State" runat="server" Width="30px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext">
                                                                            Zip:</td>
                                                                        <td class="style11">
                                                                            <asp:TextBox ID="Zip" runat="server" Width="80px" ReadOnly="True"></asp:TextBox>
                                                                        </td>
                                                                        <td class="style10">
                                                                            &nbsp;</td>
                                                                        <td>
                                                                            &nbsp;</td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="style16">
                                                                            </td>
                                                                        <td class="style17">
                                                                            </td>
                                                                        <td class="style18">
                                                                            </td>
                                                                        <td class="style19">
                                                                            </td>
                                                                        <td class="style20">
                                                                            </td>
                                                                        <td class="style21">
                                                                            </td>
                                                                        <td class="style22">
                                                                            </td>
                                                                        <td class="style23">
                                                                            </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext">
                                                                            Equipment:</td>
                                                                        <td class="style14">
                                                                            <asp:DropDownList ID="DropDownListEquip" runat="server" CssClass="maintext" 
                                                                                DataSourceID="Equipment" DataTextField="CompanyEqpDesc" DataValueField="eqpID">
                                                                            </asp:DropDownList>
                                                                        </td>
                                                                        <td class="style13">
                                                                            &nbsp;</td>
                                                                        <td class="style12">
                                                                            &nbsp;</td>
                                                                        <td class="style9">
                                                                            &nbsp;</td>
                                                                        <td class="style11">
                                                                            &nbsp;</td>
                                                                        <td class="style10">
                                                                            &nbsp;</td>
                                                                        <td>
                                                                            <asp:SqlDataSource 
                                                                                ID="Equipment" 
                                                                                runat="server" 
                                                                                ConnectionString="<%$ ConnectionStrings:HRIServiceConnectionString1 %>" 
                                                                                SelectCommand="SELECT a.Company + ' - ' + b.Description As CompanyEqpDesc, b.eqpID FROM Manufacturers a JOIN Equipment b ON a.mfgID=b.mfgID WHERE b.eqpID IN (SELECT eqpID FROM Cus_Equip WHERE cusID=@cusID)">
                                                                                <SelectParameters>
                                                                                <asp:ControlParameter 
                                                                                ControlID="DropDownList1"
                                                                                Name="cusID"
                                                                                PropertyName="SelectedValue" 
                                                                                Type="String" />
                                                                                </SelectParameters>
                                                                            </asp:SqlDataSource>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="maintext" valign="top">
                                                                            Problem:</td>
                                                                        <td class="style8" colspan="5">
                                                                            <asp:TextBox ID="Problem" runat="server" Height="100px" TextMode="MultiLine" 
                                                                        Width="400px"></asp:TextBox>
                                                                        </td>
                                                                        <td class="maintext" valign="top">
                                                                            Notes:</td>
                                                                        <td>
                                                                            <asp:TextBox ID="Notes" runat="server" Height="100px" TextMode="MultiLine" 
                                                                        Width="400px"></asp:TextBox>
                                                                        </td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td class="style7" colspan="8" valign="top">
                                                                            <asp:Button ID="Button1" runat="server" Text="Submit" />
                                                                            &nbsp;<asp:Button ID="Button2" runat="server" Text="Reset" />
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </InsertItemTemplate>
                                                        </asp:FormView>
                                                        <asp:SqlDataSource ID="SqlDataSource4" runat="server"></asp:SqlDataSource>
                                                        <br />
                                                        &nbsp;<br />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                                <asp:SqlDataSource 
                                    ID="SqlDataSource1" 
                                    runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:HRIServiceConnectionString1 %>" 
                                    SelectCommand="SELECT [cusID], [Company] FROM [Customers]">
                                </asp:SqlDataSource>
                                
                                <asp:SqlDataSource 
                                    ID="SqlDataSource2" 
                                    runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:HRIServiceConnectionString1 %>"                                
                                    SelectCommand="SELECT [cusID], [FirstName]+ ' ' + [LastName] AS contID FROM [Contacts] WHERE ([cusID] = @cusID) ORDER BY [LastName], [FirstName]">
                                    <SelectParameters>
                                        <asp:ControlParameter 
                                        ControlID="DropDownList1"
                                        Name="cusID"
                                        PropertyName="SelectedValue" 
                                        Type="String" />    
                                    </SelectParameters>
                                </asp:SqlDataSource>
                                <asp:SqlDataSource 
                                    ID="SqlDataSource3" 
                                    runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:HRIServiceConnectionString1 %>"                                
                                    SelectCommand="SELECT [UserID], [FirstName]+ ' ' + [LastName] AS techid FROM [aspnet_Users] WHERE [isTech] = 'True' ORDER BY [LastName], [FirstName]">
                                </asp:SqlDataSource>
                            </td>
                        </tr>
                    </table>
                    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
                </td>
            </tr>
        </table>
</asp:Content>

Code behind:

Partial Class AddCall
    Inherits System.Web.UI.Page
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
        Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
        DropDownList2.SelectedIndex = 0
        FirstName.Text = ""
        LastName.Text = ""
    End Sub
    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        'labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text

        Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox)
        Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
        Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox)
        Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox)
        Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
        Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
        Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox)
        Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox)
        Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox)

        Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue
        Using connection As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True")
            Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection)
            connection.Open()
            Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader()
            Try
                If reader.Read Then
                    Company.Text = reader.Item("Company").ToString
                    Address1.Text = reader.Item("Address1").ToString
                    Address2.Text = reader.Item("Address2").ToString
                    Phone.Text = reader.Item("Phone").ToString
                End If
            Finally
                ' Always call Close when done reading.
                reader.Close()
            End Try
            str_sql = "SELECT * FROM CONTACTS WHERE cusID = " & DropDownList2.SelectedValue
            command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
            reader = command.ExecuteReader()
            Try
                If reader.Read Then
                    FirstName.Text = reader.Item("FirstName").ToString()
                    LastName.Text = reader.Item("LastName").ToString()
                End If
            Catch ex As Exception
                reader.Close()
            End Try

            reader.Close()
            str_sql = "SELECT * FROM Zip WHERE cusID = " & DropDownList1.SelectedValue
            command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
            reader = command.ExecuteReader()
            Try
                If reader.Read Then
                    City.Text = reader.Item("City").ToString()
                    State.Text = reader.Item("State").ToString()
                    Zip.Text = reader.Item("Zip").ToString()
                End If
            Catch ex As Exception
                reader.Close()
            End Try
        End Using

    End Sub

    Protected Sub

        Dim id As String = Trim(Request.QueryString("id"))

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1")
        Dim cmd As New SqlCommand("SELECT * FROM Customers WHERE cusID=@cusID", conn)
        cmd.Parameters.AddWithValue("@cusID", id)
        Try
            conn.Open()
            Dim dtrReader As SqlDataReader = cmd.ExecuteReader()
            If dtrReader.HasRows Then
                'found record with that id
                While dtrReader.Read()
                    'set your textboxes here
                    tbCompanyname.Text = dtrReader("Company")
                    tbAddress1.Text = dtrReader("address1")
                    'this is sample code. follow the pattern
                End While
            Else
                'no customer found for that id. redirect.
                response.redirect("default.aspx")
            End If
            dtrReader.Close()
            conn.Close()
        Catch ex As SqlException
            response.write(ex)
        End Try

    End Sub

    Protected Sub DropDownListEquip_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

End Class

oh boy, You're not even closing connections, so that will continue to rack up until it freezes on you.

Anyway, let me make some edits and I'll get right back to you with some code.

I cannot test it over here, so there may be errors.

And that's a bad thing? JK, this is what you get when dealing with someone in month one of .NET knowledge.

Partial Class AddCall
    Inherits System.Web.UI.Page


    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
        Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
        DropDownList2.SelectedIndex = 0
        FirstName.Text = ""
        LastName.Text = ""
    End Sub


    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        'labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text

        Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox)
        Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
        Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox)
        Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox)
        Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
        Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
        Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox)
        Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox)
        Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox)

        Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue
        Using connection As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True")

            Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection)

            Try
                connection.Open()
                Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader()

                If reader.HasRows then
                    while reader.Read()
                        Company.Text = reader.Item("Company").ToString
                        Address1.Text = reader.Item("Address1").ToString
                        Address2.Text = reader.Item("Address2").ToString
                        Phone.Text = reader.Item("Phone").ToString
                    end while
                End If
                reader.Close()


                str_sql = "SELECT * FROM CONTACTS WHERE cusID = " & DropDownList2.SelectedValue
                command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
                reader = command.ExecuteReader()

                If reader.HasRows then
                    while reader.Read()
                        FirstName.Text = reader.Item("FirstName").ToString()
                        LastName.Text = reader.Item("LastName").ToString()
                    end while
                End If
                reader.Close()


                str_sql = "SELECT * FROM Zip WHERE cusID = " & DropDownList1.SelectedValue
                command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
                reader = command.ExecuteReader()

                If reader.HasRows then
                    while reader.Read()
                        City.Text = reader.Item("City").ToString()
                        State.Text = reader.Item("State").ToString()
                        Zip.Text = reader.Item("Zip").ToString()
                    end while
                End If
                reader.Close()
                connection.Close()
            Catch ex As SqlException
            End Try

        End Using

    End Sub


    Protected Sub Page_Load(ByVal S As Object, ByVal E As EventArgs)
        Dim cusID As String = Trim(Request.QueryString("cusID"))
        Dim conID As String = Trim(Request.QueryString("conID"))

        if Not cusID Is Nothing and Not conId Is Nothing and Len(cusID) > 1 and Len(conID) > 1 then
            DropDownList1.Items.FindByValue(cusID).Selected = True
            Dim eventarg As New EventArgs
            Call DropDownList1_SelectedIndexChanged(DropDownList1, eventarg.Empty)

            DropDownList2.Items.FindByValue(conID).Selected = True
            eventarg = New EventArgs
            Call DropDownList2_SelectedIndexChanged(DropDownList2, eventarg.Empty)
        end if
    End Sub

    Protected Sub DropDownListEquip_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

End Class

On that last code you posted ...

Server Error in '/HRIService' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'SqlException' is not defined.

Source Error:

Line 71: reader.Close()
Line 72: connection.Close()
Line 73: Catch ex As SqlException
Line 74: End Try
Line 75:


Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\AddCall.aspx.vb Line: 73

On the add new company page I got:

Server Error in '/HRIService' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'cusID' is not declared.

Source Error:

Line 58: End Try
Line 59:
Line 60: Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
Line 61:
Line 62: End Sub


Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\DisAddCo.aspx.vb Line: 60

I think that means your try/catch is failing, otherwise it would have been set. Here, do this code:

Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Public Class DisAddCo
    Inherits System.Web.UI.Page
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        ' Retrieve controls
        Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
        Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
        Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
        Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
        Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
        Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
        Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
        Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
        Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)

        If CompanyTextBox Is Nothing Then Return
        If FirstNameTextBox Is Nothing Then Return
        If LastNameTextBox Is Nothing Then Return
        If Address1TextBox Is Nothing Then Return
        If CityTextBox Is Nothing Then Return
        If StateTextBox Is Nothing Then Return
        If ZipTextBox Is Nothing Then Return
        If PhoneTextBox Is Nothing Then Return

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)

        Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
        cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
        cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
        cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
        cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)

        Try
            conn.Open()

            Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())

            cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
            cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)

            Dim conID As Integer = Conver.ToInt32(cmd.ExecuteScalar())

            cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
            cmd.Parameters.AddWithValue("@cusID", cusID)
            cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
            cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
            cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)

            cmd.ExecuteNonQuery()

            If conn.State = System.Data.ConnectionState.Open Then conn.Close()

            Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
        Catch ex As System.Data.SqlException
            Response.Write(ex)
        Finally
            If conn.State = System.Data.ConnectionState.Open Then conn.Close()
        End Try

    End Sub

End Class

Oh and the reason why SqlException wasn't working before is because you're doing code-behind, which means you needed:

System.Data.SqlException

New error:

Server Error in '/HRIService' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'System.Data.SqlException' is not defined.

Source Error:

Line 58:
Line 59: Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
Line 60: Catch ex As System.Data.SqlException
Line 61: Response.Write(ex)
Line 62: Finally


Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\DisAddCo.aspx.vb Line: 60

Ok, I guess I'm missing some code somewhere. I tried to add a new customer but it didn't post to the new ticket page. It just reloaded. I'm guessing it has something to do with the Page_load code.

Nope, check the page for the word "failed". Your update failed, otherwise it would have redirected. Now you have to find out how it failed.

Change line 60 and 61 to:

Catch ex As System.Data.SqlClient.SqlException
Response.write (ex)

Found the error in my code, it was that I was missing SqlClient. :\

Hate code-behind! lol

Here's the error:

Server Error in '/HRIService' Application.
Object cannot be cast from DBNull to other types.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 45: cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
Line 46:
Line 47: Dim conID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
Line 48:
Line 49: cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)


Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\DisAddCo.aspx.vb Line: 47

This article has been dead for over six months. Start a new discussion instead.