Solved

Guidance on Getting Started

Posted on 2004-08-02
7
207 Views
Last Modified: 2010-04-23
I am a long-time web developer in a myriad of different languages, including Classic ASP/VBScript. I have never been one for any sort of IDE, quite frankly, having cut my teeth developing web apps on early unix boxes using vi for an editor!

I am trying to get started with ASP.NET, and specifically VB.net, utilizing the Visual Studio .NET IDE, taking that plunge to the integrated graphical way of doing things.

Although I can find code examples (many of them that don't seem to utilize the code-behind method, which I sort of gathered was one of the points of .net), what I'm not getting is exactly how most people use this environment.

For example, how much of the visual portion of the IDE do you use? I want to learn RIGHT habits from the start. I can open up a text editor and copy/paste some of the example code I've found, but I don't want to start bad habits.

I just tried my first project, decided to create a web application with a datagrid control. I dragged a SQLDataAdapter and SQLConnection onto the work area. I have connected to my SQLServer database and 'tested' this connection. I have a pulldown field that I'd like to associate with that SQLDataAdapter which will 'drive' the selection of data into the DataGrid.  Do you generally go straight to code to do this, or through the IDE? Can you point me to a nice visual guide on how to get started on this stuff?

I am looking for best practices sort of information, want to know HOW to do, not just get code samples. I know how to program, that won't be an issue, I just want to understand what's considered "right" in this particular environment.
0
Comment
Question by:nonesuch
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11698968
Ok, first thing forget everything you know about web development.  Hehe ok maybe not everything, but seriously its quite different.  Secondly, don't drag the sql connections, adapters, etc.  Its way to bulky when done that way, and frankly its a layer of abstraction you don't need while learning.  Lastly, I will leave you with some information resources I have found priceless on a daily basis.  These are only my thoughts on the matter anyone else may disagree.

http://www.vbforums.com/

http://abstractvb.com/

http://msdn.microsoft.com/

http://www.asp.net/Forums/Default.aspx?tabindex=1&tabid=39

Its not as hard as you make it and it will save you soooo much time you'll wonder how you ever lived without it.  Can't wait for ASP.NET 2.0 Things just keep gettn better.

Regards

Aeros
0
 
LVL 3

Author Comment

by:nonesuch
ID: 11699044
So... within Studio, you start out with laying the grid layout on your HTML page, and at that point go to the code behind and start typing? Seriously? I just assumed people did a lot more of that in the IDE.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11699789
I set the IDE to Flowlayout because ppl have different resolutions so absolute doesn't work in real life.  I drag any controls I need eg....datagrids, textboxes, labels, buttons, etc.  Then I do all the databinding and program logic in the codebehind  No the  IDE really is very bad as an actual editor, lol I use dreamweaver for that.

Regards,

Aeros
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:ptakja
ID: 11705577
Personally, I like the Visual Studio IDE.  The 2003 edition has finally matured to the point where I find it's features on par with some other 3rd party editors.  I do a mix of projects, both Web & Windows Forms.  I find that the IDE meets all my needs.
0
 
LVL 3

Author Comment

by:nonesuch
ID: 11705648
Okay, guess no one else is going to chime in on this one so I'll keep asking you. Have you ever actually worked the way they allege this is supposed to work well -- i.e., someone else modifies your page design and you handle the code behind? Just curious.

Have one more question (about how to get something to work) then I'll award points. Thanks!

So, I've got a Web form app that has a simple pulldown (against table company to look up all the various companies) and a datagrid that has listing of contacts.

What I want to have happen is when you first hit the page, the pulldown is at "default" position, and the datagrid lists all contacts for all companies. The datagrid has paging enabled, and you can resort by clicking on the column headings. That worked for awhile. Now I added in the pulldown so that (my intention) when you selected a different company, it would re-bind the datagrid with just that company's contacts.

What I'm missing is how, in this scenario, you "retain" the original settings. When I page, it goes back to the default query and sort. I added a label to update with the current sql statement so I could see what was happening. In "classic" ASP I would save my variables as hidden form fields, but I'm not clear on the preferred technique for doing this in vb.net.


Here's the code:


PAGE:

<body MS_POSITIONING="FlowLayout">
      <form id="Form1" method="post" runat="server">
            <asp:Label id="Label2" runat="server" Font-Size="X-Small" Font-Names="Verdana">Select Company:</asp:Label>
            <asp:DropDownList id="DropDownList2" DataValueField="company_id" DataTextField="company_name" runat="server"></asp:DropDownList>
            <HR width="100%" SIZE="1">
            <asp:Label id="Label1" runat="server" Font-Size="X-Small" Font-Names="Verdana">Current Where:</asp:Label>
            <asp:TextBox id="WhereLabel" runat="server" Height="112px" Width="504px" TextMode="MultiLine"></asp:TextBox>
            <asp:DataGrid id="DataGrid1" runat="server" Width="600px" Height="136px" BackColor="#C0C0FF" BorderColor="#FFFFC0" BorderStyle="Double" Font-Names="Verdana" Font-Size="XX-Small" AllowSorting="True" AllowPaging="True"
OnPageIndexChanged="Page_Change" onSortCommand="SortCommand_OnClick" PagerStyle-Mode="NextPrev"
PagerStyle-NextPageText="Next->" PagerStyle-PrevPageText="<-Prev" PageSize="20">
            <SelectedItemStyle BackColor="Yellow"></SelectedItemStyle>
            <AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
            <HeaderStyle ForeColor="Purple" BackColor="#8080FF"></HeaderStyle>
            <PagerStyle NextPageText="Next-&gt;" PrevPageText="&lt;-Prev"></PagerStyle>
            </asp:DataGrid>
      </form>
</body>

VB Code Behind:

Imports System.Data.SqlClient


Public Class WebForm1
      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 SqlConnection1 As System.Data.SqlClient.SqlConnection
      Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
      Protected WithEvents Button1 As System.Web.UI.WebControls.Button
      Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
      Protected WithEvents DataSet11 As KTest.DataSet1
      Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
      Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
      Protected WithEvents Label2 As System.Web.UI.WebControls.Label
      Protected WithEvents Label1 As System.Web.UI.WebControls.Label
      Protected WithEvents WhereLabel As System.Web.UI.WebControls.TextBox

      '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

      Dim BaseSQLStmt As String = "select u.user_id, c.company_name, u.user_name, u.password, " _
       & "u.first_name, u.last_name from user u inner join company c on " _
       & "c.company_id = u.company_id "
      Protected SQLStmt As String
      Protected WhereStmt As String
      Protected SortStmt As String

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            If Not Page.IsPostBack Then
                  SQLStmt = BaseSQLStmt & WhereStmt & SortStmt
                  BindGrid()
            End If

      End Sub

      Sub Page_Change(ByVal sender As System.Object, ByVal e As DataGridPageChangedEventArgs)
            Dim start As Integer
            start = DataGrid1.CurrentPageIndex * DataGrid1.PageSize
            SQLStmt = BaseSQLStmt & WhereStmt & SortStmt
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            BindGrid()
      End Sub

      Private Function GetDR(ByVal sqlText As String) As SqlDataReader
            Dim dr As SqlDataReader
            Dim sqlConn As SqlConnection = New SqlConnection(connectionstring)
            Dim sqlCmd As SqlCommand = New SqlCommand(sqlText, sqlConn)
            sqlCmd.Connection.Open()
            dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
            Return dr
      End Function

      Sub BindGrid()

            WhereLabel.Text = SQLStmt

            DropDownList2.DataSource = GetDR("select company_id, company_name from company order by company_name")
            DropDownList2.DataBind()

            Dim myConnection As SqlConnection = New SqlConnection(connectionstring)
            Dim ds As DataSet = New DataSet
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(SQLStmt, myConnection)
            adapter.Fill(ds, "users")
            DataGrid1.DataSource = ds.Tables("users").DefaultView
            DataGrid1.DataBind()

      End Sub

      Sub SortCommand_OnClick(ByVal Source As System.Object, ByVal e As DataGridSortCommandEventArgs)
            SortStmt = " order by " & e.SortExpression
            SQLStmt = BaseSQLStmt & WhereStmt & SortStmt
            WhereLabel.Text = SQLStmt
            BindGrid()
      End Sub

      Sub PagerButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            SQLStmt = BaseSQLStmt & WhereStmt & SortStmt
            WhereLabel.Text = SQLStmt
            BindGrid()
      End Sub


      Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
            WhereStmt = " where u.company_id = " & CType(DropDownList2.SelectedValue, String)
            SQLStmt = BaseSQLStmt & WhereStmt & SortStmt
            WhereLabel.Text = SQLStmt
            BindGrid()
      End Sub
End Class

0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11705725
You need to recall your binding durning page load for it to rebind based on the new value, although I think that should be a different question.

As for your original question, I guess that would be ideal to have a graphic designer to do the aspx part for you, but I work for a mid size ISP and we only have 1 graphic artist in house, so I usually just include the graphics header, menues, footers etc.  I you want a more complete answer to your new question please post it seperately and I would be happy to elaborate.

Regards,

Aeros
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 200 total points
ID: 11705739
Just to go the extra mile, heres code to do what you described:

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

That should clear everything up for you:

Regards,

Aeros
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

16 Experts available now in Live!

Get 1:1 Help Now