Read column data from MSSQL and output on page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • movieking81
    New Member
    • Feb 2007
    • 52

    Read column data from MSSQL and output on page

    I think I'm making a assumption about C#, that is works similar to VBscript. I'm new to C# and I believe I'm going about this bit of code the wrong way. If some one can take a look and maybe point in the right direction that would be great. I'm trying to read data from a MSSQL database/table and display one of the columns on the page. Of course, I have removed the names for security.


    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="somepage.aspx.cs" Inherits="xxx" %>
    
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Xml" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <%
        SqlConnection con = null;
        SqlCommand cmd = null;
        SqlDataReader rd = null;
    
        con = new SqlConnection("server=xxx.xxx.xxx.xxx; database=namehere;user=userid;password=password");
        cmd = new SqlCommand("SELECT * FROM tablename", con);
    
        con.Open();
        rd = cmd.ExecuteReader();
        rd.Read();
    %>
    
        <table><tr><td>Name:<%Response.Write(rd.GetString(1));%></td></tr></table>
    
        <%
            rd.Close();
            con.Close();  
        %>
    The error I'm getting is "The name 'rd' does not exist in the current context". If I put the code together within the same tags it works fine. Ca you not break up the C# code? Should I be using VB to code this page instead? I think I just answered by own question.

    Thanks

    Dean
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Database How-to parts 1 and 2
    Database tutorial Part 1
    Database tutorial Part 2

    Comment

    • movieking81
      New Member
      • Feb 2007
      • 52

      #3
      Thanks, I can follow the tutorials and like I said I can get it to work fine if all of the C# code is put together. I guess my question is can you inter-mix standard html code with C#? Like what is done with VBscript. I don't seem to see that explained in the tutorials.

      Thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You can but it gets messy.

        There's a lot to be said about keeping your C# code in it's own file, separate from the UI component (the ASPX page). I'm not going to get into it right now.

        What exactly are you trying to do?
        Do you want to add dynamic JavaScript to your page?
        If so check out How to use JavaScript in ASP.NET

        If you trying to do database related stuff then check out the links previously posted.

        -Frinny

        Comment

        • movieking81
          New Member
          • Feb 2007
          • 52

          #5
          I'm building a timesheet app and I need to fill in textboxes with the table data so the user can update it. I'd like to display the data in a table, hence the html. Is there a different avenue I should pursue? I would include all the code together and build the table with "Response.Write ", but there are dropdown controls in the table that are being filled with XML files.

          Thanks for the help

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Don't use Response.Write (especially do not use it in your C# code). I understand why people who come from a classic ASP background are so attached to it but in ASP.NET it is rarely needed.

            What you're trying to do is pretty straight forward. Use something like the GridView control which will bind to data that you retrieve from you database and automatically creates the grid/table (it's actually rendered as an HTML table) for you.

            The GridView has a lot of features that will help you accomplish your task. Like editing, paging, etc. It also lets you specify how your data should be displayed so that if you want to use a RadioButtonList or a DropDownList in the table to display the data you can (you would use a TemplateField for this task) .

            It takes some getting used to. I recommend using MSDN Library to search for any questions you have on the topic before asking here.

            Cheers!

            -Frinny

            Comment

            Working...