Filling textboxes from database - ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soonerpgh
    New Member
    • Apr 2008
    • 23

    Filling textboxes from database - ASP.NET

    Hello, all. I am building an ASP application where we have a table named Clients and I am wanting to display a specific client record on a form with textboxes. In other words, I want to have the first name in one box, middle name in another, last name in another, you get the idea. I will also need to be able to update these records, as well as add new records using these textboxes. All I have tried so far has failed. Can someone give me some guidance in this task? Any assistance will be greatly appreciated. Thanks.
    Last edited by jhardman; Apr 3 '08, 11:51 AM. Reason: moved to .Net forum. ASP forum is for "classic" ASP
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Originally posted by soonerpgh
    Hello, all. I am building an ASP application where we have a table named Clients and I am wanting to display a specific client record on a form with textboxes. In other words, I want to have the first name in one box, middle name in another, last name in another, you get the idea. I will also need to be able to update these records, as well as add new records using these textboxes. All I have tried so far has failed. Can someone give me some guidance in this task? Any assistance will be greatly appreciated. Thanks.
    You will need to do 4 main things once your HTML form is built.

    1) Set up a connection to your database
    All connection strings in one place. Find the syntax for your database connection using ADO.NET, ADO, ODBC, OLEDB, C#, VB, VB.NET, ASP.NET and more.


    2) Read that database via an SQL script to load the data into variables on your page.
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    3) Set the form objects values equal to the variables you set from the database
    4) On the save you will need to re-get the user entered data from the forms via the request.form syntax and then use SQL Update to update your record in the database
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    To do all this you will also need to learn about ASP in general and how to use its syntax to accomplish things on the actual page
    Sorry! We can't seem to find the resource you're looking for

    Comment

    • soonerpgh
      New Member
      • Apr 2008
      • 23

      #3
      At the moment I have a SqlDataSource configured and the form is built, of course. Wouldn't the SqlDataSource take care of the connection?

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        Originally posted by soonerpgh
        At the moment I have a SqlDataSource configured and the form is built, of course. Wouldn't the SqlDataSource take care of the connection?
        Does that mean you are using ASP.NET ?

        The direction I gave you was specifically for Classic ASP.

        If you are using .NET its a whole new ball game for what you will need to do.

        Comment

        • soonerpgh
          New Member
          • Apr 2008
          • 23

          #5
          Yes, I am using ASP.Net. Sorry I didn't specify that before. I just assumed you all could read my mind. My apologies.

          Comment

          • jeffstl
            Recognized Expert Contributor
            • Feb 2008
            • 432

            #6
            Originally posted by soonerpgh
            Yes, I am using ASP.Net. Sorry I didn't specify that before. I just assumed you all could read my mind. My apologies.
            OK.

            Please post the specific errors or problems and the relevant code and I will take a look.

            If you need a place to start though with .NET I would take a look at the tutorials here.

            Comment

            • soonerpgh
              New Member
              • Apr 2008
              • 23

              #7
              There isn't much to it, actually, because I really don't know where to start. Everything I find on databinding is referring to the data controls. Anyway, here is my test code:
              [code=asp]
              <%@ Page Language="VB" %>

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

              <script runat="server">

              </script>

              <html xmlns="http://www.w3.org/1999/xhtml">
              <head runat="server">
              <title>Test Page</title>
              </head>
              <body>
              <form id="form1" runat="server">
              <div>

              &nbsp;<asp:Labe l ID="Label1" runat="server" Text="First Name:"></asp:Label>
              &nbsp;

              <asp:TextBox ID="txtFName" runat="server"> </asp:TextBox>
              <br />
              <br />
              <asp:Button ID="btnSave" runat="server" CommandName="Up date" Text="Save" />
              <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
              ConnectionStrin g="<%$ ConnectionStrin gs:FUND235v2.0a ConnectionStrin g %>"

              SelectCommand=" SELECT FirstName FROM [tbl_Client] WHERE ([ClientID] = @ClientID)"
              UpdateCommand=" UPDATE tbl_Client SET ([FirstName} = @FirstName) WHERE ([ClientID] = @ClientID) ">
              <SelectParamete rs>
              <asp:ControlPar ameter ControlID="Hidd enField1" Name="ClientID"
              PropertyName="V alue" />
              </SelectParameter s>
              <UpdateParamete rs>
              <asp:Paramete r Name="ClientID" />
              </UpdateParameter s>
              </asp:SqlDataSour ce>

              <asp:HiddenFiel d ID="HiddenField 1" runat="server" Value="7"/>

              </div>
              </form>
              </body>
              </html>[/code]
              Last edited by Frinavale; Apr 14 '08, 07:29 PM. Reason: added [code] tags

              Comment

              • jeffstl
                Recognized Expert Contributor
                • Feb 2008
                • 432

                #8
                OK the methods you are using may work but they are not what I am used to.

                For example I don't see a Sub Page_Load on your page, which is the first event that will fire as the page loads.

                Also you placed your text box out there and gave it an ID of txtFirstName, but you did not bind that to anything.

                To be honest I am not familiar with the way you are doing it, using only asp tags without any code-behind.

                However, if you put something like this in your script area it should get your started. Keep in mind you will also need to figure out your connection string for this.

                Code:
                 Sub Page_Load
                     dim DBCommand,strsql,DBReader
                    ConnectDB()
                     if not page.ispostback
                          '  SET SQL
                            strsql = "Select * from Clients"
                            DBCommand = New OleDbCommand(strsql, DBConnection)
                            DBReader = DBCommand.ExecuteReader()
                            DBReader.Read()
                  
                            txtFirstName.Text = DBReader("first_name")
                     end if
                     CloseDB()
                End Sub
                Also, if you want here is an example of how I set up my database connections, except I use MS Access right now....

                Code:
                'SUB DATABASE CONNECTIONS
                sub ConnectDB
                  DBConnection = New OleDbConnection( _
                    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & Request.ServerVariables("APPL_PHYSICAL_PATH") & "..\db\Mydata.mdb")
                  DBConnection.Open()
                end Sub
                
                sub CloseDB
                  DBConnection.Close()
                end sub
                Then I can just place ConnectDB and CloseDB wherever I need to in my page

                Hopefully this helps you out.

                Comment

                • soonerpgh
                  New Member
                  • Apr 2008
                  • 23

                  #9
                  Thanks for the suggestion. I have the conncetion string worked out, that isn't a problem.

                  As far as the "code behind" statement you made, I have VS set to put all the code in one file. I can still use VB, it is just aplied in the script area as you stated. I will look closely at what you did here and attempt to implement the idea and see where it takes me. Thanks again.

                  Comment

                  • jeffstl
                    Recognized Expert Contributor
                    • Feb 2008
                    • 432

                    #10
                    Originally posted by soonerpgh
                    Thanks for the suggestion. I have the conncetion string worked out, that isn't a problem.

                    As far as the "code behind" statement you made, I have VS set to put all the code in one file. I can still use VB, it is just aplied in the script area as you stated. I will look closely at what you did here and attempt to implement the idea and see where it takes me. Thanks again.

                    Ah yes. I sometimes forget about VS. I use hand coding and dreamweaver to do almost everything.

                    Yes you can store you code-behind in a .vb file, sorry.

                    Comment

                    • waterfall
                      New Member
                      • Dec 2007
                      • 22

                      #11
                      is ur problem solved?

                      Comment

                      • soonerpgh
                        New Member
                        • Apr 2008
                        • 23

                        #12
                        Originally posted by waterfall
                        is ur problem solved?
                        Nope, still trying to get that going.

                        Comment

                        • soonerpgh
                          New Member
                          • Apr 2008
                          • 23

                          #13
                          A new twist on this one... can someone show me how to simply pull the data, say from a data grid to load into variables? If I can accomplish that, the rest is a piece of cake.

                          Comment

                          • jeffstl
                            Recognized Expert Contributor
                            • Feb 2008
                            • 432

                            #14
                            Originally posted by soonerpgh
                            A new twist on this one... can someone show me how to simply pull the data, say from a data grid to load into variables? If I can accomplish that, the rest is a piece of cake.
                            Currently (still learning asp.net a little) I only know of 2 ways you can do this.

                            One is to use the OnItemCommand property of a Data grid. This event is called\fired whenever a ButtonColumn is clicked on your data grid (Example: A delete button)

                            [code=asp]
                            'first part is code-behind for the event when the button link is clicked
                            'second 'part is the actual data grid

                            Sub ClientDataComma nds(sender as Object, InvGrid As DataGridCommand EventArgs)
                            dim MyClientNameVar as TableCell = ClientData.Item .Cells(1)
                            'now you have the client name from the row that was clicked by the user
                            'loaded into MyClientNameVar
                            End Sub


                            <asp:DataGrid id="ClientData " runat="server"
                            AutoGenerateCol umns="False" CellPadding="0"
                            HeaderStyle-BackColor="#A4A 6CF"
                            HeaderStyle-ForeColor="0000 33"
                            HeaderStyle-Font-Size="12"
                            HeaderStyle-HorizontalAlign ="Left"
                            HeaderStyle-Font-Bold="True"
                            BackColor="#eee eee" Width="27%"
                            HorizontalAlign ="Left"
                            Font-Bold="True" Font-Name="Tahoma"
                            Font-Size="8pt"
                            OnItemCommand=" ClientDataComma nds">
                            <Columns>
                            <asp:BoundColum n HeaderText="Del ete." ItemStyle-BackColor="#FFF FFF" ItemStyle-ForeColor="#990 000" />
                            <asp:ButtonColu mn DataTextField=" client_name"
                            </Columns>
                            </asp:DataGrid>

                            [/code]

                            The other way is to simply use the same exact SQL you used to populate your grid and read the data from the reader instead of binding it to a data grid control

                            Keep in mind this code here is assuming you will only be returning 1 record from your SQL query. If you have multiple you will have to deal with that somehow
                            [code=asp]
                            strsql = "SELECT * FROM ClientTable where ClientID = " & ClientID & ""
                            DBCommand = New OleDbCommand(st rsql, DBConnection)
                            DBReader = DBCommand.Execu teReader()
                            If DBReader.Read then
                            MyClientNameVar = DBReader("clien t_name")
                            'now you can continue to load all columns from the table
                            End While
                            DBReader.Close
                            [/code]

                            Comment

                            • soonerpgh
                              New Member
                              • Apr 2008
                              • 23

                              #15
                              OK, I am getting really annoyed with myself on this, as I seem to be wasting the time of others as well as my own with my incorrect terminology.

                              To correct my prior post, I am using a Gridview control, not a Datagrid. Please forgive my mistake. By the way, that last solution looked promising, maybe now that I have the right info out here, you guys can either kick me to the gutter, if you wish, or maybe some smart individual will hit on the solution. I know somwone, somewhere has done this. I just need to find that person and pick his/her brain.

                              Again, sorry for the confusion.

                              Comment

                              Working...