473,815 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET / SQL Server 2005 Database interaction:loc alhost

Dököll
2,364 Recognized Expert Top Contributor
ASP.NET and SQL Server 2005 Database interaction includes:

Language: ASP.NET
Connectivity: SQL Server 2005

Foreword: This database connectivity is nothing of genius, it is a simple connection, with SQL statements and VB code. It will require some knowledge of the VS Web Dev 2005 Express platform to make more sense of this Article.

TIPS: Visit/Download free software and videos to familiarize with these VS Web Dev 2005 editions, a friend referred me to this:-)

If you have Visual Studio 2005 installed:

(1) Start a new ASP.NET Web site
(2) Give folder name of choice
(3) Accept all defaults, including adding a Master Page
(4) Say okay
(5) Add a form, call it DataCentral
(6) Add 6 textboxes and one button
(7) Add an SQL database, call it SQLDB, add it to the App_Data folder
(8) Allow Nulls for only the IP address being returned in this database
(9) Add 6 fields all together in table (read code to figure out how to name fields)
(7) Add Table by right-clicking on Tables in Database Explorer
(8) Save and Exit database design screen

Expand|Select|Wrap|Line Numbers
  1.  
  2. Protected Sub Add2SQLDB_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  3.  
  4.         Dim SQLDBDataSource  As New SqlDataSource
  5.         SQLDBDataSource .ConnectionString = ConfigurationManager.ConnectionStrings("SQLDBConnectionString1").ToString
  6.         SQLDBDataSource .InsertCommandType = SqlDataSourceCommandType.Text
  7.         SQLDBDataSource .InsertCommand = "INSERT INTO LIBRARY(EmailAddress, IPAddress, DateTimeStamp, itemPrice, itemName, typeApartment, crimeRate1, crimeRate2) VALUES(@EmailAddress,@IPAddress, @DateTimeStamp, @itemPrice, @itemName, @typeApartment, @crimeRate1, @crimeRate2)"
  8.         SQLDBDataSource .InsertParameters.Add("EmailAddress", emailAddressTextBox.Text)
  9.         SQLDBDataSource .InsertParameters.Add("IPAddress", Request.UserHostAddress.ToString())
  10.         SQLDBDataSource .InsertParameters.Add("DateTimeStamp", DateTime.Now())
  11.         SQLDBDataSource .InsertParameters.Add("itemPrice", itemPriceTextbox.Text)
  12.         SQLDBDataSource .InsertParameters.Add("itemName", itemNameTextbox.Text)
  13.         SQLDBDataSource .InsertParameters.Add("typeApartment", typeApartmentTextbox.Text)
  14.         SQLDBDataSource .InsertParameters.Add("crimeRate1", crimeRate1Textbox.Text)
  15.         SQLDBDataSource .InsertParameters.Add("crimeRate2", crimeRate2Textbox.Text)        
  16.  
  17.         Dim rowsAffected As Integer = 0
  18.         Try
  19.             rowsAffected = SQLDBDataSource.Insert()
  20.         Catch ex As Exception
  21.             Server.Transfer("Error_Submitting.aspx")
  22.  
  23.         Finally
  24.             SQLDBDataSource = Nothing
  25.         End Try
  26.  
  27.         If rowsAffected <> 1 Then           
  28.             Server.Transfer("Error_Submitting.aspx")
  29.         Else
  30.             Server.Transfer("OK_Submitting.aspx")
  31.         End If
  32.  
  33.  
  34.  
  35.     End Sub
  36. </script>
  37.  
  38. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  39.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  40.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  41.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  42.     &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  43.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Admin Apartment Log<br />
  44.     <br />
  45.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  46.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  47.     &nbsp;
  48.     Email Address:<asp:TextBox ID="emailAddressTextBox" runat="server"></asp:TextBox>
  49.     <asp:RegularExpressionValidator ID="regExValidator" runat="server" ControlToValidate="emailAddressTextBox"
  50.         ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator><br />
  51.     <br />
  52.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  53.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  54.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Price:
  55.     <asp:TextBox ID="itemPriceTextbox" runat="server"></asp:TextBox><br />
  56.     <br />
  57.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  58.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  59.     &nbsp; &nbsp; &nbsp; &nbsp; ItemName:
  60.     <asp:TextBox ID="itemNameTextbox" runat="server"></asp:TextBox><br />
  61.     <br />
  62.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  63.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  64.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type:
  65.     <asp:TextBox ID="typeApartmentTextbox" runat="server"></asp:TextBox><br />
  66.     <br />
  67.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  68.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  69.     Crime Rate # 1:
  70.     <asp:TextBox ID="crimeRate1Textbox" runat="server"></asp:TextBox><br />
  71.     <br />
  72.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  73.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  74.     Crime Rate # 2:
  75.     <asp:TextBox ID="crimeRate2Textbox" runat="server"></asp:TextBox><br />
  76.     <br />
  77.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  78.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  79.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  80.     &nbsp; &nbsp; &nbsp; &nbsp;
  81.     <asp:Button ID="Button1" runat="server" Height="26px" Text="Submit" Width="101px" OnClick="Button1_Click" /><br />
  82.     <br />
  83.     <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" Width="224px" />
  84.     <br />
  85. </asp:Content>
  86.  
  87.  
  88.  
Please set validator for email field to fend off bogus emails.

See Next Page


Master Page

(1) Master page holds look and feel of additional web site pages
(2) Master page can be used to change colour or background of pages

Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="VB" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6.  
  7. </script>
  8.  
  9. <html xmlns="http://www.w3.org/1999/xhtml" >
  10. <head runat="server">
  11.     <title>Master Page</title>
  12. </head>
  13. <body>
  14.     <form id="form1" runat="server">
  15.     <div>
  16.         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  17.         </asp:contentplaceholder>
  18.     </div>
  19.     </form>
  20. </body>
  21. </html>
  22.  
View Data Page Next...

View Data Page

(1) Right-click on path to your project
(2) Add New Item
(3) Choose Web form
(4) Call it OK_Submitting
(5) Say Yes to Master Page to it is included
(6) Right-click on path to your project again
(7) Hit Add New Item
(8) Choose Dataset
(9) Name it whatever you want

(a) Say Yes when you get a pop up
(b) Your database should now be listed in dropdown
(c) Hit All defaults, Next, Next, Next
(d) When asked to type up query, use 'Query Builder' instead
(e) Add database table being used in this project
(f) Choose all colums in Query builder view
(g) Say OK, then Finish
(h) Go back to OK_Submitting page
(i) Choose desgin view
(j) Drag from your left, under Data, a Gridview (Gridview1 as default)
(k) Bind to DataSource using Choose Data Source
(l) Choose Object in Data Source Configuration wizard pop up
(m) Bind to YourTableNameTa bleAdapter option from dropdown
(n) Hit Next, if not already selected, choose only option in dropdown
(o) Hit Next, then Finish


Now you can add code below...

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/DashMaster.master" Title="Thank You Page" %>
  2.  
  3. <script runat="server">
  4.  
  5. Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  6.  
  7. End Sub
  8.  
  9.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  10.         If Button1.Enabled Then
  11.             Server.Transfer("SubmitPage.aspx")
  12.             'Else
  13.             'Server.Transfer("OK_Submitting.aspx")
  14.         End If
  15.     End Sub
  16. </script>
  17.  
  18. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  19.     Why, Thank you!<br />
  20.     <br />
  21.     Please take a look at your submissions<br />
  22.     <br />
  23.     &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  24.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  25.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  26.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
  27.     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
  28.         AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" Width="231px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
  29.         <Columns>
  30.             <asp:CommandField ShowSelectButton="True" />
  31.             <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" SortExpression="EmailAddress" />
  32.         </Columns>
  33.     </asp:GridView>
  34.     &nbsp;<br />
  35.     &nbsp;&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go Back to Add more Data"
  36.         Width="205px" /><br />
  37.     &nbsp; &nbsp; &nbsp;<br />
  38.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
  39.         SelectMethod="GetData" TypeName="DataSet1TableAdapters.EmailTableAdapter">
  40.     </asp:ObjectDataSource>
  41.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  42.     &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
  43.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  44.     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
  45.     &nbsp;<br />
  46.     <br />
  47.     &nbsp;
  48.     <br />
  49. </asp:Content>
  50.  
  51.  
Problem submitting page next...

Problem Submitting Page

(1) Right-click on path to your project
(2) Add New Item
(3) Choose Web form
(4) Call it Error_Submittin g
(5) Say Yes to Master Page to it is included
(6) Right-click on path to your project again
(7) Drag a button on Error_Submittin g form
(8) Called Button1 as default
(9 Add code below

Code allows user to go back and fix problem...


Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/DashMaster.master" Title="Problem Page" %>
  2.  
  3. <script runat="server">
  4.  
  5.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  6.         If Button1.Enabled Then
  7.             Server.Transfer("SubmitPage.aspx")
  8.         End If
  9.     End Sub
  10. </script>
  11.  
  12. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  13.     There was a problem...<br />
  14.     <br />
  15.     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go back and Fix" />
  16. </asp:Content>
  17.  
  18.  
For more information on this and other ASP.NET examples, please visit Microsoft's ASP.NET home page via Get Started The Official Microsoft ASP_NET Site

http://www.asp.net/get-started/

ASP.NET, The End...
Jan 22 '08 #1
0 7591

Sign in to post your reply or Sign up for a free account.

Similar topics

2
42882
by: Asad Khan | last post by:
Hello, I was using MySQL till a few days ago just fine. Now everytime I try to run it using mysql.exe, I get the following error: "can't connect to mysql server 'localhost' (10061)" What can I do to get rid of this? (What is 10061?)
14
1871
by: John Spiegel | last post by:
Hi all, I'm missing something, probably stupid, on connecting to a SQL Server database from an aspx file. I haven't really done much w/ SQL Server and suspect that it's a problem on that side. I've installed the SQL Server developer edition locally and do have a pubs database. The connection I'm attempting is the following: SqlConnection objConnection =
3
1634
by: Joey | last post by:
Hello all, I develop on two or three different machines at different times. I need a connection string in my app that will always reference the local instance (for whatever machine) of Microsoft SQL Server 2005. I have found that the following will work, but I have to change the machine name as I code on each box... "server=MACHINENAME;database=MYDATABASE;uid=sa;password=;"
1
1361
by: keithb | last post by:
I developed a web site that uses sql server express for permissions and roles. Now I need to deploy it on a server that uses sql server 2005 standard edition instead of sql server express. What do I need to change so that permissions and roles for my website use sql server 2005 standard edition instead of sql server express? Thanks, Keith
8
6717
by: nick | last post by:
I have only SQL Server 2005 installed on my PC. And I tried to add the following rows in web.config to use SQL Server 2005 instead of Express: <connectionStrings> <clear /> <add name="LocalSqlServer" connectionString="Data Source=.\SQL2005;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;user instance=true;Integrated Security=True;Initial Catalog=ASPNETDB;" providerName="System.Data.SqlClient" /> </connectionStrings>
2
9392
by: 111mike | last post by:
Hello, Here's my problem. I cannot connect to mysql database using odbc string connections or dns. I keep getting a "cannot connect to mysql server localhost." I'm running windows XP Pro and have installed IIS as my server. I have installed Mysql 5.0 and mysql ODBC driver 3.51.12 in their default locations. Database is up and running okay, as I've been able to create databases and tables and access them via the command prompt and...
1
13816
by: bg_ie | last post by:
Hi, I like to develope an application which uses a MsSQL database. I believe that C# works really well with MsSQL. To start with, I'd like to host the sql server locally on my machine as I develop, but later I'd like to be able to host it on a server and have multiple instances of the application access the server. I have installed SQL Server 2005 Express Edition on my machine but was never prompted for a user name or anything like...
2
3984
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: D:\original\CaseStudy-2-5\CaseStudy\Day02\exercise>asant database Buildfile: build.xml env-user: prop-user: set-user:
3
2593
by: teo | last post by:
I'm developing a software (a WinApp, not a WebApp) to retrieve Date & Time from a web server, but firstly I need to to test it on my PC, with "localhost" I'm using the VB Net language (below two different code approaches), I'm with XP Sp2 and IIS (all IIS http and ftp tasks regularly work) I have no firewall
0
9737
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10673
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10430
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9227
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5712
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4360
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3889
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.