Declaring the SqlConnection and SqlCommand objects in a webform without CodeBehind
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Declaring the SqlConnection and SqlCommand objects in a webform without CodeBehind

  1. #1
    Join Date
    Sep 2010
    Posts
    3

    Declaring the SqlConnection and SqlCommand objects in a webform without CodeBehind

    I am trying to query data from a MS SQL server with a C# web form without “CodeBehind”. I want to write the information to a table in the web form’s body.
    I am having trouble declaring the SqlConnection and SqlCommand objects that connect to the server.

    How do you use “using System;”, “using System.Data;”, “using System.Data.SqlClient” inside of script element of as .aspx file?


    I know how to do it with JavaScript and ADO. (code below)

    <html><head><table>
    <%@ language="javascript" %>
    <%
    var sqlstr = " ";
    var conn_str = " ";
    var AdOpenForwardOnly = 0;
    var adLockReadOnly = 1;
    var rs = new ActiveXObject("ADODB.Recordset");
    var conn = new ActiveXObject("ADODB.Connection");

    conn_str = "PROVIDER=SQLOLEDB;Server=my_server;" +
    "Database=db_myDatabase;User ID=myUserID;" +
    "Password=myPassword;"

    sqlstr = "SELECT * FROM [myTable]";

    conn.open(conn_str, "", "");
    rs.open(sqlstr, conn, AdOpenForwardOnly, adLockReadOnly);

    while(!rs.eof)
    {
    Response.write("<tr><td>" + rs(0));
    Response.write("</td><td>"+ rs(1));
    Response.write("</td><td>"+ rs(2));
    Response.write("</td></tr>");

    rs.MoveNext();
    }
    %></tbody></table></body></html>


    Any suggestions, links to articles or tutorials would be greatly appreciated.

  2. #2
    Join Date
    Sep 2010
    Posts
    3

    Talking Re: Declaring the SqlConnection and SqlCommand objects in a webform without CodeBehin

    Solution:

    http://localhost:1532/Default.aspx?myage=12

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <!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>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="Data
    Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDatabase.mdf;Integrated
    Security=True;User Instance=True"
    ProviderName="System.Data.SqlClient"
    SelectCommand="SELECT * FROM [tblMain] WHERE ([age] &gt; @age)">
    <SelectParameters>
    <asp:QueryStringParameter DefaultValue="0" Name="age"
    QueryStringField="myage" Type="String" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1">
    <Columns>
    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
    <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
    <asp:BoundField DataField="number" HeaderText="number" SortExpression="number" />
    </Columns>
    </asp:GridView>
    </div>
    </form>
    </body></html>

  3. #3
    Join Date
    Sep 2010
    Posts
    5

    Re: Declaring the SqlConnection and SqlCommand objects in a webform without CodeBehin

    What about a drag and drop SqlDataSource from the tool box?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center