Datasource question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slinky

    #1

    Datasource question

    I have a practice website (I'm a newbie) that I want to use the
    Northwind SQL Server sample database in. First, do I upload the
    database into my App_Data folder of my website? Also I have a
    textbook .aspx I'm doing a lab on with the following code (what I'm
    wondering is do I change the "local" to the complete URL of my
    website? Also should I drag the Northwind object over my .aspx form to
    create the Data Adapter or is that handled in the code below? Also in
    HTML code following, where should I place A and where should I place
    B?):

    A) Imports System.Data
    Imports.System. Data.SqlClient

    B) Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles MyBase.Load
    If IsPostBack Then
    Dim cnn As SqlConnection(" Data Source=(local); " & _
    New SqlConnection(" Data Source=(local); " & _
    "Initial Catalog=Northwi nd;" & _
    "Integrated Security=SSPI")
    Dim ds As DataSet = New DataSet()
    Dim da As SqlAdapter = _
    New SqlDataAdapter( )
    Dim cmdSelect As SqlCommand = _
    cnn.CreateComma nd()
    cmdSelect.Comma ndType = CommandType.Tex t
    cmdSelect.Comma ndText = _
    "SELECT CustomerID, CompanyName, " & _
    "ContactNam e FROM Customers"
    Dim cmdInsert As SqlCommand = _
    cnn.CreateComma nd()
    cmdInsert.Comma ndType = CommandType.Tex t
    cmdInsert.Comma ndText = _
    "INSERT INTO Customers " & _
    "(CustomerI D, CompanyName, ContactName) " & _
    "VALUES(@Custom erID, @CompanyName, _
    "@ContactNa me)"
    cmdInsert.Param eters.Add("@Cus tomerID", _
    SqlDbType.NChar , _
    5, "CustomerID ")
    cmdInsert.Param eters.Add("@Com panyName", _
    SqlDbType.NVarC har, _
    40, "CompanyNam e")
    cmdInsert.Param eters.Add("@Con tactName", _
    SqlDbType.NVCha r, _
    30, "ContactNam e")
    cmdInsert.Param eters("@Custome rID"), _
    SourceVersion = _
    DataRowVersion. Original
    Da.SelectComman d = cmdSelect
    Da.InsertComman d = cmdInsert
    Da.Fill(ds, "Customers" )
    Dim dr As DataRow = ds.Tables( _
    "Customers").Ne wRow()
    Dr(0) = txtCustomerID.T ext
    Dr(1) = txtCompanyName. Text
    Dr(2) = txtContactName. Text
    Ds.Tables("Cust omers").Rows.Ad d(dr)
    Da.Update(ds, "Customers" )
    lblResults.Text = "Row added!"
    End If
    End Sub



    <%@ 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>Untitl ed Page</title>
    </head>
    <body bgcolor="#00ffc c">
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtCustomer ID" runat="server" Style="z-index:
    100; left: 256px; position: absolute;
    top: 240px" BorderStyle="Ri dge"></asp:TextBox>
    <asp:TextBox ID="txtCompanyN ame" runat="server" Style="z-
    index: 101; left: 256px; position: absolute;
    top: 272px"></asp:TextBox>
    <asp:Button ID="btnAdd" runat="server" Style="z-index: 102;
    left: 344px; position: absolute;
    top: 360px" Text="Add" Width="128px"
    OnClick="btnAdd _Click" />
    <asp:TextBox ID="txtContactN ame" runat="server" Style="z-
    index: 103; left: 256px;
    position: absolute; top: 304px"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 104; left: 104px; position: absolute; top:
    240px" Text="Customer ID"
    Width="120px"></asp:Label>
    <asp:Label ID="Label2" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 105; left: 104px; position: absolute; top:
    304px" Text="Contact Name"
    Width="120px"></asp:Label>
    <asp:Label ID="Label3" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 106; left: 104px; position: absolute; top:
    272px" Text="Company Name"
    Width="120px"></asp:Label>
    <asp:Label ID="Label4" runat="server" BackColor="Whit e"
    ForeColor="Blac k" Style="z-index: 108;
    left: 88px; position: absolute; top: 432px"
    Text="[lblResults]" Width="440px"></asp:Label>

    </div>
    </form>
    </body>
    </html>

  • Michel Bechelani

    #2
    Re: Datasource question

    First, do I upload the database into my App_Data folder of my website?

    I would do so.
    do I change the "local" to the complete URL of my
    website?
    If your SQL Server is in the same box as your web server you can leave
    local. Otherwise write the name of the server and instance name. Example:
    MYSERVER\SQLEXP RESS
    >where should I place A and where should I place
    B?):
    Are you using Visual Studio to build your web site? If the Page_Load method
    should be there as default when you create a new ASPX page with code-behind
    enable. If youd using a text editor then the proper way to put A is above
    the class and namespace declaration and B goes inside the class, since it's
    a method.

    Hope this helps,

    Regards,

    Michel Bechelani

    --
    This post is provided "AS IS" with no explicit or implied warranties.

    "slinky" <campbellbrian2 001@yahoo.comwr ote in message
    news:1178077713 .031162.262560@ l77g2000hsb.goo glegroups.com.. .
    >I have a practice website (I'm a newbie) that I want to use the
    Northwind SQL Server sample database in. First, do I upload the
    database into my App_Data folder of my website? Also I have a
    textbook .aspx I'm doing a lab on with the following code (what I'm
    wondering is do I change the "local" to the complete URL of my
    website? Also should I drag the Northwind object over my .aspx form to
    create the Data Adapter or is that handled in the code below? Also in
    HTML code following, where should I place A and where should I place
    B?):
    >
    A) Imports System.Data
    Imports.System. Data.SqlClient
    >
    B) Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles MyBase.Load
    If IsPostBack Then
    Dim cnn As SqlConnection(" Data Source=(local); " & _
    New SqlConnection(" Data Source=(local); " & _
    "Initial Catalog=Northwi nd;" & _
    "Integrated Security=SSPI")
    Dim ds As DataSet = New DataSet()
    Dim da As SqlAdapter = _
    New SqlDataAdapter( )
    Dim cmdSelect As SqlCommand = _
    cnn.CreateComma nd()
    cmdSelect.Comma ndType = CommandType.Tex t
    cmdSelect.Comma ndText = _
    "SELECT CustomerID, CompanyName, " & _
    "ContactNam e FROM Customers"
    Dim cmdInsert As SqlCommand = _
    cnn.CreateComma nd()
    cmdInsert.Comma ndType = CommandType.Tex t
    cmdInsert.Comma ndText = _
    "INSERT INTO Customers " & _
    "(CustomerI D, CompanyName, ContactName) " & _
    "VALUES(@Custom erID, @CompanyName, _
    "@ContactNa me)"
    cmdInsert.Param eters.Add("@Cus tomerID", _
    SqlDbType.NChar , _
    5, "CustomerID ")
    cmdInsert.Param eters.Add("@Com panyName", _
    SqlDbType.NVarC har, _
    40, "CompanyNam e")
    cmdInsert.Param eters.Add("@Con tactName", _
    SqlDbType.NVCha r, _
    30, "ContactNam e")
    cmdInsert.Param eters("@Custome rID"), _
    SourceVersion = _
    DataRowVersion. Original
    Da.SelectComman d = cmdSelect
    Da.InsertComman d = cmdInsert
    Da.Fill(ds, "Customers" )
    Dim dr As DataRow = ds.Tables( _
    "Customers").Ne wRow()
    Dr(0) = txtCustomerID.T ext
    Dr(1) = txtCompanyName. Text
    Dr(2) = txtContactName. Text
    Ds.Tables("Cust omers").Rows.Ad d(dr)
    Da.Update(ds, "Customers" )
    lblResults.Text = "Row added!"
    End If
    End Sub
    >
    >
    >
    <%@ 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>Untitl ed Page</title>
    </head>
    <body bgcolor="#00ffc c">
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtCustomer ID" runat="server" Style="z-index:
    100; left: 256px; position: absolute;
    top: 240px" BorderStyle="Ri dge"></asp:TextBox>
    <asp:TextBox ID="txtCompanyN ame" runat="server" Style="z-
    index: 101; left: 256px; position: absolute;
    top: 272px"></asp:TextBox>
    <asp:Button ID="btnAdd" runat="server" Style="z-index: 102;
    left: 344px; position: absolute;
    top: 360px" Text="Add" Width="128px"
    OnClick="btnAdd _Click" />
    <asp:TextBox ID="txtContactN ame" runat="server" Style="z-
    index: 103; left: 256px;
    position: absolute; top: 304px"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 104; left: 104px; position: absolute; top:
    240px" Text="Customer ID"
    Width="120px"></asp:Label>
    <asp:Label ID="Label2" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 105; left: 104px; position: absolute; top:
    304px" Text="Contact Name"
    Width="120px"></asp:Label>
    <asp:Label ID="Label3" runat="server" BackColor="#004 040" Font-
    Bold="True" ForeColor="Whit e"
    Style="z-index: 106; left: 104px; position: absolute; top:
    272px" Text="Company Name"
    Width="120px"></asp:Label>
    <asp:Label ID="Label4" runat="server" BackColor="Whit e"
    ForeColor="Blac k" Style="z-index: 108;
    left: 88px; position: absolute; top: 432px"
    Text="[lblResults]" Width="440px"></asp:Label>
    >
    </div>
    </form>
    </body>
    </html>
    >

    Comment

    Working...