Solved

Databinding to a simple form.

Posted on 2004-08-03
7
692 Views
Last Modified: 2007-12-19
Hello,

I come from a classic ASP background.  I understand the general principles of ASP.Net but I hit a brick wall when it comes to databinding.  It seems many other would be asp.net people are in the same boat as me.

I am trying to solve this stumbling block by working with an example based on a simple asp.net 1.1 Framework web form.  

All of the tutorials I've seen on the Net (and msdn) so far seem to want you to use a datagrid, listbox or repeater. I would prefer not to use them at all.

Based on this simple scenario can someone lay it out for me?

Here's what I got:
------------------------------------------------------------------------------------

I  am using the SQL Server Northwinds Sample Database with the 'Orders' Table

On my Webform I have a single textbox1 (for now) that is to contain the Orders table field called: Shipname.

I have a SQLAdapter1, a SQLConnector1 and  a Dataset1 based on from 'SELECT * FROM ORDERS'

My goals are:

1. Bind the single field 'Shipname' in the Orders Table to textbox1.

2. Create navigate buttons that go to First, Prev, Next, Last, Save/Update, and Create a new Record.

What do I need to do to make this work?

Once I have this I can move on.

Thank you!

Ted
0
Comment
Question by:tcalbaz
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
For a datagird with Paging:

Public Class ViewJobs
    Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents dg As System.Web.UI.WebControls.DataGrid
    Protected WithEvents SearchProducts As System.Web.UI.WebControls.Button
    Protected WithEvents FieldsToMatch As System.Web.UI.WebControls.DropDownList
    Protected WithEvents SearchQuery As System.Web.UI.WebControls.TextBox
    Protected WithEvents FullQuery As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindTheData()
    End Sub
    Private Sub BindTheData(Optional ByVal Query As String = "")
        Dim cnn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet
        If Query = "" Then Query = FullQuery.Value
        If Query = "" Then
            Query = "SELECT * FROM JobNumbers ORDER BY JobNumber DESC"
        End If
        cmd.CommandText = Query
        cmd.Connection = cnn
        cnn.Open()
        da.SelectCommand = cmd
        da.Fill(ds, "JobNumbers")
        dg.DataSource = ds.Tables("JobNumbers").DefaultView
        dg.DataKeyField = "JobNumberID"
        dg.DataBind()
        cnn.Close()
        cmd.Dispose()
        cnn.Dispose()
    End Sub
    Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dg.PageIndexChanged
        dg.CurrentPageIndex = e.NewPageIndex
        Dim Query As String
        Query = "SELECT * FROM JobNumbers WHERE JobNumbers."
        Select Case FieldsToMatch.SelectedValue
            Case "JobNumber"
                Query &= "JobNumber LIKE '%" & SearchQuery.Text & "%'"
            Case "CustomerID"
                Query &= "CustomerID LIKE '%" & SearchQuery.Text & "%'"
            Case "Either"
                Query &= "JobNumber LIKE '%" & SearchQuery.Text & "%' " & _
                "OR JobNumber.CustomerID LIKE '%" & SearchQuery.Text & "%'"
        End Select
        Query &= " ORDER BY JobNumbers.JobNumberID DESC"
        BindTheData(Query)
        FullQuery.Value = Query
    End Sub
    Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand
        If e.CommandName = "Edit" Then
            Response.Redirect("EditJob.aspx?jnid=" & CStr(dg.DataKeys(e.Item.ItemIndex)))
        End If
    End Sub
    Private Sub SearchProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchProducts.Click
        Dim Query As String
        Query = "SELECT * FROM JobNumbers WHERE JobNumbers."
        Select Case FieldsToMatch.SelectedValue
            Case "JobNumber"
                Query &= "JobNumber LIKE '%" & SearchQuery.Text & "%'"
            Case "CustomerID"
                Query &= "CustomerID LIKE '%" & SearchQuery.Text & "%'"
            Case "Either"
                Query &= "JobNumber LIKE '%" & SearchQuery.Text & "%' " & _
                "OR JobNumber.CustomerID LIKE '%" & SearchQuery.Text & "%'"
        End Select
        Query &= " ORDER BY JobNumbers.JobNumberID DESC"
        BindTheData(Query)
        FullQuery.Value = Query
    End Sub
End Class

For a clickable Datalist With Custom Paging:

<%@ Page language="vb"  Inherits="DOTNETShoppingCart.PagingDataList" CodeBehind="PagingDataList.aspx.vb" AutoEventWireup="false" %>
<HTML>
      <HEAD>
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <style>
.GridHeader TD { PADDING-RIGHT: 20px; FONT-WEIGHT: bold; BORDER-BOTTOM: black 2px solid; WHITE-SPACE: nowrap }
.GridItem TD { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; PADDING-BOTTOM: 5px; CURSOR: hand; PADDING-TOP: 5px; BORDER-BOTTOM: white 1px solid; FONT-FAMILY: Courier New; WHITE-SPACE: nowrap; BACKGROUND-COLOR: #ffffff; asp: Literal id="ItemLetterSpacing" runat="server"></asp:Literal> --> }
.GridItemAlternate TD { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; PADDING-BOTTOM: 5px; CURSOR: hand; PADDING-TOP: 5px; BORDER-BOTTOM: white 1px solid; FONT-FAMILY: Courier New; WHITE-SPACE: nowrap; BACKGROUND-COLOR: #f3f3f3; asp: Literal id="AltItemLetterSpacing" runat="server"></asp:Literal> --> }
.GridItemOver TD { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 5px; CURSOR: hand; PADDING-TOP: 5px; FONT-FAMILY: Courier New; WHITE-SPACE: nowrap; BACKGROUND-COLOR: #fffdce }
.GridItemInactive TD { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-WEIGHT: normal; PADDING-BOTTOM: 5px; CURSOR: hand; PADDING-TOP: 5px; BORDER-BOTTOM: white 1px solid; FONT-FAMILY: Courier New; WHITE-SPACE: nowrap; BACKGROUND-COLOR: #d5dbe1; asp: Literal id="InactiveItemLetterSpacing" runat="server"></asp:Literal> --> }
            </style>
            <script language="javascript">
<!--
function openLocation(newlocation) {
      if (newlocation.length != 0)
            window.location = newlocation;
      else
            return;
}
//-->
            </script>
      </HEAD>
      <body>
            <form runat="server">
                  <h3>
                        <H3><asp:datalist id="dtlProducts" runat="server" Width="738px" RepeatColumns="1" DataKeyField="ProductID">
                                    <HeaderTemplate>
                                          <table width="100%" cellspacing="0" cellpadding="0">
                                                <tr class="Gridheader">
                                                      <th align="left" width="15%">
                                                            Image</th>
                                                      <th align="left" width="25%">
                                                            Product Name</th>
                                                      <th align="left" width="45%">
                                                            Description</th>
                                                      <th align="left" width="15%">
                                                            Price</th>
                                                </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                          <tr id="ItemRow" runat="server" class="GridItem" onmouseover="this.className='GridItemOver';"
                                                onmouseout="this.className='GridItem';" onclick='<%# "openLocation(""ProductDetail.aspx?ProductID=" & DataBinder.Eval(Container.DataItem, "ProductID") & """);" %>'>
                                                <td><img src='<%# DataBinder.Eval(Container.DataItem, "ImageSrc", "Pictures/Products/Thumbnails/{0}")%>'></td>
                                                <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
                                                <td><%# DataBinder.Eval(Container.DataItem, "Description") %></td>
                                                <td><%# DataBinder.Eval(Container.DataItem, "Price") %></td>
                                          </tr>
                                    </ItemTemplate>
                              </asp:datalist></H3>
                  </h3>
                  <table width="100%">
                        <tr>
                              <td align="left" width="42%"><asp:label id="lblCounts" Runat="server" CssClass="plaintable"></asp:label></td>
                              <TD vAlign="bottom" width="4%"><A id="First" href="pagingdatalist.aspx#BookMark" runat="server" OnServerClick="ShowFirstPage"><IMG src="images/firstpage.gif" border="0"></A>
                              </TD>
                              <TD vAlign="bottom" width="4%"><A id="Previous" href="pagingdatalist.aspx#BookMark" runat="server" OnServerClick="ShowPreviousPage"><IMG src="images/prevpage.gif" border="0"></A>
                              </TD>
                              <TD vAlign="bottom" width="4%"><A id="Next" href="pagingdatalist.aspx#BookMark" runat="server" OnServerClick="ShowNextPage"><IMG src="images/nextpage.gif" border="0"></A>
                              </TD>
                              <TD vAlign="bottom" width="4%"><A id="Last" href="pagingdatalist.aspx#BookMark" runat="server" OnServerClick="ShowLastPage"><IMG src="images/lastpage.gif" border="0"></A>
                              </TD>
                              <TD width="42%">&nbsp;</TD>
                        </tr>
                  </table>
                  <asp:label id="lblRecordCount" Runat="server" Visible="False"></asp:label><asp:label id="lblCurrentIndex" Runat="server" Visible="False" Text="0"></asp:label><asp:label id="lblPageSize" Runat="server" Visible="False" Text="10"></asp:label>
                  <H3>&nbsp;</H3>
            </form>
      </body>
</HTML>

Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration

Public Class PagingDataList : Inherits System.Web.UI.Page

    Protected WithEvents First As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents Previous As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents dtlProducts As System.Web.UI.WebControls.DataList
    Protected WithEvents lblCounts As System.Web.UI.WebControls.Label
    Protected WithEvents lblRecordCount As System.Web.UI.WebControls.Label
    Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
    Protected WithEvents lblPageSize As System.Web.UI.WebControls.Label
    Protected WithEvents Last As System.Web.UI.HtmlControls.HtmlAnchor

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack() Then
            BindTheData()
        End If
    End Sub

    Private Sub BindTheData()
        Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim DataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM Products", cnn)
        Dim DataSet As New DataSet
        Dim PageSize As Integer = 10
        cnn.Open()
        If Not Page.IsPostBack() Then
            DataAdapter.Fill(DataSet)
            lblRecordCount.Text = CStr(DataSet.Tables(0).Rows.Count)
            DataSet = Nothing
            DataSet = New DataSet
        End If
        DataAdapter.Fill(DataSet, CInt(lblCurrentIndex.Text), CInt(lblPageSize.Text), "Products")
        dtlProducts.DataSource = DataSet.Tables("Products").DefaultView
        dtlProducts.DataBind()
        cnn.Close()
        ShowCounts()
        cnn.Dispose()
        DataAdapter.Dispose()
    End Sub

    Public Sub ShowFirstPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        lblCurrentIndex.Text = "0"
        BindTheData()
    End Sub

    Public Sub ShowPreviousPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        lblCurrentIndex.Text = CStr(CInt(lblCurrentIndex.Text) - CInt(lblPageSize.Text))
        If CInt(lblCurrentIndex.Text) < 0 Then
            lblCurrentIndex.Text = "0"
        End If
        BindTheData()
    End Sub

    Public Sub ShowNextPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        If CInt(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text)) < CInt(lblRecordCount.Text) Then
            lblCurrentIndex.Text = CStr(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text))
        End If
        BindTheData()
    End Sub

    Public Sub ShowLastPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        Dim intMod As Integer
        intMod = CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)
        If intMod > 0 Then
            lblCurrentIndex.Text = CStr(CInt(lblRecordCount.Text) - intMod)
        Else
            lblCurrentIndex.Text = CStr(CInt(lblRecordCount.Text) - CInt(lblPageSize.Text))
        End If
        BindTheData()
    End Sub

    Private Sub ShowCounts()
        lblCounts.Text = "|Total Rows: <b>" & lblRecordCount.Text
        lblCounts.Text += "</b> | Page:<b> "
        lblCounts.Text += CStr(CInt(CInt(lblCurrentIndex.Text) / CInt(lblPageSize.Text) + 1))
        lblCounts.Text += "</b> of <b>"
        If (CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)) > 0 Then
            lblCounts.Text += CStr(CInt(CInt(lblRecordCount.Text) / CInt(lblPageSize.Text) + 1))
        Else
            lblCounts.Text += CStr(CInt(lblRecordCount.Text) / CInt(lblPageSize.Text))
        End If
        lblCounts.Text += "</b> |"
    End Sub
    Private Sub InitializeComponent()

    End Sub
   
    Private Sub dtlProducts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtlProducts.ItemDataBound
        Dim TR As HtmlTableRow
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            TR = CType(e.Item.FindControl("ItemRow"), Web.UI.HtmlControls.HtmlTableRow)
            If e.Item.ItemType = ListItemType.AlternatingItem Then
                TR.Attributes("onmouseout") = "this.className='GridItemAlternate';"
                TR.Attributes("class") = "GridItemAlternate"
            End If
            If CStr(DataBinder.Eval(e.Item.DataItem, "ProductID")) = "0" Then
                TR.Attributes("class") = "GridItemInactive"
                TR.Attributes("onmouseout") = "this.className='GridItemInactive';"
            End If
        End If
    End Sub

    Private Sub dtlProducts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtlProducts.SelectedIndexChanged

    End Sub
End Class

If you need the .aspx code for the standard datagrid let me know, but Its just that standard, you can drag and drop it.

Regards,

Aeros
0
 
LVL 1

Author Comment

by:tcalbaz
Comment Utility
Aeros,

Thanks for the quick reply.  But I am attempting to create a form that does not use datagrids, repeaters or datalist:

".... All of the tutorials I've seen on the Net (and msdn) so far seem to want you to use a datagrid, listbox or repeater. I would prefer not to use them at all......"

Do you have an example for just a simple form using a single textbox?

Thank You

Ted


0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Here ya go with validation of course:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="EditJob.aspx.vb" Inherits="Synthesis.EditJob"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>EditJob</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="FlowLayout">
            <form id="Form1" method="post" runat="server">
                  <P>
                        <asp:Label id="Label1" runat="server" Font-Size="Large" Font-Bold="True" ForeColor="#4980B9">Edit existing job</asp:Label>
                        <asp:Label id="lblJobNumberID" runat="server" Visible="False">JobNumberID</asp:Label>
                        <HR width="100%" SIZE="1">
                  <P></P>
                  <P>
                        <TABLE id="Table1" style="WIDTH: 481px; HEIGHT: 146px" cellSpacing="1" cellPadding="1"
                              width="481" border="1">
                              <TR>
                                    <TD>
                                          <asp:Label id="Label2" runat="server" Font-Bold="True" Width="160px">Job Number:</asp:Label></TD>
                                    <TD>
                                          <asp:Label id="lblJobNumber" runat="server">JobNumber</asp:Label></TD>
                              </TR>
                              <TR>
                                    <TD>
                                          <asp:Label id="Label3" runat="server" Font-Bold="True">CustomerID</asp:Label></TD>
                                    <TD>
                                          <asp:TextBox id="txtCustomerID" runat="server" Width="304px"></asp:TextBox></TD>
                              </TR>
                              <TR>
                                    <TD>
                                          <asp:Label id="Label4" runat="server" Font-Bold="True">StartDate(mm/dd/yyyy)</asp:Label></TD>
                                    <TD>
                                          <asp:TextBox id="txtStartDate" runat="server" Width="304px"></asp:TextBox></TD>
                              </TR>
                              <TR>
                                    <TD>
                                          <asp:Label id="Label5" runat="server" Font-Bold="True">EndDate(mm/dd/yyyy)</asp:Label></TD>
                                    <TD>
                                          <asp:TextBox id="txtEndDate" runat="server" Width="304px"></asp:TextBox></TD>
                              </TR>
                              <TR>
                                    <TD>
                                          <asp:Label id="Label6" runat="server" Font-Bold="True">AssignedStaff</asp:Label></TD>
                                    <TD>
                                          <asp:TextBox id="txtAssignedStaff" runat="server" Width="304px"></asp:TextBox></TD>
                              </TR>
                              <TR>
                                    <TD>
                                          <asp:Label id="Label7" runat="server" Font-Bold="True">Description</asp:Label></TD>
                                    <TD>
                                          <asp:TextBox id="txtDescription" runat="server" Width="304px" Height="194px" TextMode="MultiLine"
                                                MaxLength="200"></asp:TextBox></TD>
                              </TR>
                              <tr>
                                    <td>
                                          <asp:ValidationSummary id="ValidationSummary1" runat="server" Width="168px" Height="34px" HeaderText="Please review the following"></asp:ValidationSummary></td>
                                    <td align="right"><INPUT id="Submit1" type="submit" value="Submit" name="Submit1" runat="server"></td>
                              </tr>
                        </TABLE>
                  </P>
                  <P>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="CustomerID is a required field"
                              ControlToValidate="txtCustomerID" Display="None"></asp:RequiredFieldValidator>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" Visible="False" ErrorMessage="StartDate is a required field"
                              ControlToValidate="txtStartDate" Display="None"></asp:RequiredFieldValidator>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" Visible="False" ErrorMessage="EndDate is a required field"
                              ControlToValidate="txtEndDate" Display="None"></asp:RequiredFieldValidator></P>
                  <P>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" Visible="False" ErrorMessage="AssignedStaff is a required field"
                              ControlToValidate="txtAssignedStaff" Display="None"></asp:RequiredFieldValidator>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator5" runat="server" ErrorMessage="Description is a required field"
                              ControlToValidate="txtDescription" Display="None"></asp:RequiredFieldValidator></P>
            </form>
      </body>
</HTML>

Public Class EditJob
    Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label7 As System.Web.UI.WebControls.Label
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents lblJobNumber As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblJobNumberID As System.Web.UI.WebControls.Label
    Protected WithEvents txtCustomerID As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtStartDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtEndDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtAssignedStaff As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
    Protected WithEvents Submit1 As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator4 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator5 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.lblJobNumberID.text = CInt(Request.QueryString("jnid"))
        Catch ex As Exception
            Response.Redirect("ManageProducts.aspx")
        End Try
        If Not IsPostBack Then LoadProductData(Me.lblJobNumberID.Text)
    End Sub
    Private Sub LoadProductData(ByVal JobNumberID As Integer)
        Dim cnn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New SqlClient.SqlCommand
        Dim dr As SqlClient.SqlDataReader
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM JobNumbers " & _
        "WHERE JobNumberID = " & JobNumberID.ToString()
        cmd.Connection = cnn
        cnn.Open()
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        dr.Read()
        Me.lblJobNumber.Text = CStr(dr("JobNumber"))
        Me.txtCustomerID.Text = CStr(dr("CustomerID"))
        Me.txtStartDate.Text = CStr(dr("StartDate"))
        Me.txtEndDate.Text = CStr(dr("EndDate"))
        Me.txtAssignedStaff.Text = CStr(dr("AssignedStaff"))
        Me.txtDescription.Text = CStr(dr("Description"))
        cnn.Close()
        dr.Close()
        cmd.Dispose()
        cnn.Dispose()
    End Sub
    Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
        Page.Validate()
        If Me.IsValid Then
            Dim cnn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New SqlClient.SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "UPDATE Jobnumbers SET JobNumber = " & Me.lblJobNumber.Text & _
        ", CustomerID = " & Me.txtCustomerID.Text & ", StartDate = '" & Me.txtStartDate.Text & _
        "', EndDate = '" & Me.txtEndDate.Text & "', AssignedStaff = '" & Me.txtAssignedStaff.Text & _
        "', Description = '" & Me.txtDescription.Text & "' WHERE JobNumberID = " & Me.lblJobNumberID.Text
        cmd.Connection = cnn
        cnn.Open()
        cmd.ExecuteNonQuery()
        cnn.Close()
        cmd.Dispose()
        cnn.Dispose()
            Response.Redirect("ViewJobs.aspx")
        End If
    End Sub
End Class

Hope that clears it up:

Regards,

Aeros
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Accepted Solution

by:
FRIBBLE1 earned 500 total points
Comment Utility
Hi i think your first problem is your trying to databind to a text box using a dataset.

The dataset is a complet set of records conting lots of difrent values you can't uses it on a text box. With the text box you need to return a single value and make the text property equal to it.

you do it like this. (I haven''t got my laptop infront of me. So i don't have vs on had but this is pritty clos)

(if your using access then uses odbcconection)

imports system.data
imports system.data.sqlclint.

Dim cmd as new sqlcommand
Dim myconn as new sqlconection

sqlconnection = "your conection string"

'i don't have a copy of northwind in front of me but you get the idea

cmd.commandtext = "SELECT Shipname FROM ORDERS WHERE orderid = 3"
cmd.conection = myconn

'opens the conection to the db
myconn.open

'the exutes the sql comand and returns the first value in oure case shipname at id 3

me.textbox1.text = cmd.executscaler()

myconn.close

That should set the text box to display the shipname.

If you are retuning more than one record at a time you can't uses the text box. I ushall uses the cobobox to display this type of thing then get my user to select the one they want. You can bind a combobox like this.

for this one you need the dataset and dataadapter.

imports system.data
imports system.data.sqlclint.

Dim cmd as new sqlcommand
Dim myconn as new sqlconection
Dim mydataset as new dataset

sqlconnection = "your conection string"

cmd.commandtext = "SELECT * FROM ORDERS"
cmd.conection = myconn

'creats the dataadapter and sets its command and conection to the same as cmd
dim mydataadapter as new sqldataadpter(cmd)

'opens the conection to the db
myconn.open
mydataadapter.fill(mydataset)
myconn.close

'now will put the data in the combo box

me.dropdownlist1.datasorce = mydataset
'set this to equal the order id colum so something like
me.dropdownlist1.datavalue = "orderid"
'this one will display the text
me.dropdownlist1.datatext = "shipname"

'bindsthedata.
me.dropdownlist1.databind()

Not shore about the nav buttons with out vs infront of me. One way might be to build an array of all the id fileds in a table then more throw them 1 by one excutiung a new select statment each time.

Hope this helps



















0
 
LVL 1

Author Comment

by:tcalbaz
Comment Utility
Fribble1,

I think we're on the same wavelength : )

Yes, it is true that I will be returning multiple records so I will probally need the dataset and dataadapter.  I also intend to eventually expand to many fields on my form.

And as you surmised I will definitely be needing a means to navigate through records with First:Prev:Next:Last and then a means to Save/Update and Add/Delete  records.  

So I will be patient and wait until you have a chance to illustrate those too.

I am using a simple model like this so that I can grasp all of the concepts

Thank You,

Ted
.
0
 
LVL 1

Author Comment

by:tcalbaz
Comment Utility
Valuable Tips,
Thank you Fribble!

Ted
0
 
LVL 1

Expert Comment

by:FRIBBLE1
Comment Utility
Sorry i haven't got back to you for a while have been very busie at work and din't relly feel like looking at more code when i got home. Are yoiu still haveing problems getting a a forward and back button working?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now