Working with individual records rather than DataGrid

Last post 01-03-2008 11:40 PM by CLIPER. 3 replies.

Sort Posts:

  • Working with individual records rather than DataGrid

    01-03-2008, 3:23 AM

    Hi there,

    I'm very much new to ASP.NET, my history is almost entirely with PHP/MySQL. Anyway at work we're running Windows Server 2003 onto which I've installed MySQL and the .Net Framework for an ASP.NET web application I'm building.

    My question is this:

    Although I now have my database set up correctly and can pull all my data out using a DataGrid in ASP.NET, I (although having run numberous Google queries) have been unable to find a quick and straight-forward guide on how to deal with the data on a record-by-record basis rather than just spluting out a load of data onto the page in a table.


    I.e. Say I do a select statement to pull all records in a table where the Primary Key is X, from this I would then like to individually deal with the data records this query gives.

    e.g.

    </P> <P>&lt;%</P> <P>My initial code to pull data from MySQL&nbsp;</P> <P>%&gt;&nbsp;</P> <P>
    &lt;b&gt;Name: &lt;/b&gt;&lt;% [resultname] %&gt;
    &lt; /br &gt;
    &lt;b&gt;Favourite Colour&lt;/b&gt;&lt;% [resultcolour] %&gt;</P> <P>

     

    Doing the above in PHP is VERY simple - yet with ASP.NET it seems to be too much to ask - but I'm sure there must be a simple way?
     

    Filed under: , ,
  • Re: Working with individual records rather than DataGrid

    01-03-2008, 7:58 AM
    • Loading...
    • CLIPER
    • Joined on 07-18-2004, 5:54 AM
    • Philippines
    • Posts 373

    Hi,

    I'm not sure if I understand your question correctly. As what I understand and as what I've been doing in php is most like dealing with variables in asp.net.

    Well, you could also do that in asp.net, but at least.. many options in the side of .NET, We can use profiles instead. OR we can use object datasources so perform queries in your UI.

    My idea is to use datareader and then its up to you if you want to use variables but you can directly use ReaderName("FieldName") in your case... :)

    Cheers,

    CLIPER

    ------------------------------------------------
    Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own commonsense. ---Buddha (563BC-483BC)
  • Re: Working with individual records rather than DataGrid

    01-03-2008, 8:40 AM

    Hi,

    Thanks for your reply, sorry I wasn't clearer. What I need to be able to do is easily grab data individually so that I can lay it wherever I like, so I could have a page that says

    Hello [mysqldata]! How are you. I see you have eaten [mysqldata] pies today.

     

    Also, I need to be able to write back to the database easily.

     

    In PHP what I would do is call my data using a simple sql select statement

    assign the data to variables like so

    1    $query = mysql_query("SELECT * FROM MyTable WHERE ID = '1'");
    2 $grab_results = mysql_fetch_row($query);
    3 $Name = $grab_results[3];
    4 $Pies_eaten = $grab_results[4];

    then feed that information into say a HTML form:-

     Name: <INPUT name=name value="<?PHP echo $Name; ?>">
     Pies: <INPUT name=pies value="<?PHP echo $Pies_eaten; ?>">

     

    -----

     

    This is what I currently have (feeding into a DataGrid)...

     

    <%@ Page Language="VB" debug="true" %>
    <%@ Import Namespace = "System.Data" %>
    <%@ Import Namespace = "MySql.Data.MySqlClient" %>
    <script language="VB" runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)

        Dim myConnection  As MySqlConnection
        Dim myDataAdapter As MySqlDataAdapter
        Dim myDataSet     As DataSet

        Dim strSQL        As String
        Dim iRecordCount  As Integer

        myConnection = New MySqlConnection("server=localhost; userid=root; password=***; database=test; pooling=false;")

        strSQL = "SELECT * FROM Names;"

        myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
        myDataSet = New Dataset()
        myDataAdapter.Fill(myDataSet, "mytable")

        MySQLDataGrid.DataSource = myDataSet
        MySQLDataGrid.DataBind()

    End Sub

    </script>

    <html>
    <head>
    <title></title>
    </head>
    <body>

    <form runat="server">

    <asp:DataGrid id="MySQLDataGrid" runat="server" />

    </form>

    </body>
    </html>

     

    -------

     

    **EDIT: If I have the data fed into variables (as I am acustomed to with PHP) it should also be easy to feed the data back into an INSERT statement?

     


     

  • Re: Working with individual records rather than DataGrid

    01-03-2008, 11:40 PM
    • Loading...
    • CLIPER
    • Joined on 07-18-2004, 5:54 AM
    • Philippines
    • Posts 373

    Hi,

    Your asp.net code is good. well, all you need to do is to bind the data to something else... well, of course there are many options in getting the data.. I suggest to use Repeater control to bind your data. And I could not post a sample here since it is well presented in the Documentation.. but if you have the ASP.NET Quickstart Tutorials then it should be fine...

    "**EDIT: If I have the data fed into variables (as I am acustomed to with PHP) it should also be easy to feed the data back into an INSERT statement? "

    Yes of course..

    Well, I could not agree & suggest anymore in using variables since we could use data controls to bind our data in our UI. As what I've posted at the top, there are many options to choose. We only need to choose the right one to fit in our app solution. Of course, I also suggest to use Profiles instead to maximize your productivity.

     

    Cheers,

    CLIPER 

    ------------------------------------------------
    Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own commonsense. ---Buddha (563BC-483BC)
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter