ASPX & Database: SQL Add new Items
Tutorials and code examples using ASP and ASP.Net
6 posts
Page 1 of 1
Hello because all good reavtions on previous tutorial here another 
Original:
First of all make a database: Named dbPersons ( I used acces 2010 ):

Edit: Don't use "-" or spaces only"_" and also when the first colum is "Id" chance it to " ID" because web developer sometimes may give an error.
Then save the table as "tblPersons". Then don't just save the db but publish ( if using acces 2007 or a later version ):

Then go to you're microsoft Visual Web Developer and Create a new aspx project. I named it "20110304-Codenstuff-aspxdatareader"
then u search you're db file " dbPersons" and drag it in App_Data:

U can also see I deleted some files. But that isn't necesairy. Then we are going to add a page named "SQLadd.aspx"

So now the coding can begin !
Lets start with getting the messy stuff out of the page! Delete the first sententce and replace it with:

Now we are first going some textboxes. It's the amount of tables in youre db in mine I've got 4:"Name, Prename, E-Mail and Country'
So I need to add 4 Textboxes:
Now also I've made 2 Submit buttons one to add and then a confirm one ;) :


Now we will start coding
:
Then u can try it ;)
New Database: Zip file: Rar file: Credits:
My teacher who taught me and my class.

Original:
First of all make a database: Named dbPersons ( I used acces 2010 ):

Edit: Don't use "-" or spaces only"_" and also when the first colum is "Id" chance it to " ID" because web developer sometimes may give an error.
Then save the table as "tblPersons". Then don't just save the db but publish ( if using acces 2007 or a later version ):

Then go to you're microsoft Visual Web Developer and Create a new aspx project. I named it "20110304-Codenstuff-aspxdatareader"
then u search you're db file " dbPersons" and drag it in App_Data:

U can also see I deleted some files. But that isn't necesairy. Then we are going to add a page named "SQLadd.aspx"

So now the coding can begin !
Lets start with getting the messy stuff out of the page! Delete the first sententce and replace it with:
Code: Select all
So u'll gain this:<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
</script>

Now we are first going some textboxes. It's the amount of tables in youre db in mine I've got 4:"Name, Prename, E-Mail and Country'
So I need to add 4 Textboxes:
Code: Select all
U see I made a css class beacuse its more professional ;) Here is the css <div>
<p><asp:Label ID="Label2" runat="server" Text="Name: "></asp:Label><asp:TextBox ID="Name" runat="server" CssClass="Textboxes"></asp:TextBox></p>
<p><asp:Label ID="Label1" runat="server" Text="Prename: "></asp:Label><asp:TextBox ID="Prename" runat="server" CssClass="Textboxes"></asp:TextBox></p>
<p><asp:Label ID="Label3" runat="server" Text="E-Mail: "></asp:Label><asp:TextBox ID="EMail" runat="server" CssClass="Textboxes"></asp:TextBox></p>
<p><asp:Label ID="Label4" runat="server" Text="Country: "></asp:Label><asp:TextBox ID="Country" runat="server" CssClass="Textboxes"></asp:TextBox></p>
</div>

Code: Select all
Add this in the <head> tag ;)<style type="text/css">
.Textboxes {position:absolute; left:75px; }
</style>
Now also I've made 2 Submit buttons one to add and then a confirm one ;) :
Code: Select all
Now It will look like this:<asp:button runat="server" text="Add" ID="Button2" onclick="Unnamed2_Click" />
<asp:Button ID="Button1" runat="server" Text="Confirm" onclick="Unnamed1_Click" Visible="False" />


Now we will start coding

Code: Select all
Or
Protected Sub Unnamed1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("App_Data\dbPersons.mdb")
'Specify path of Db and Db Type
Dim cn As New OleDbConnection(strConn)
'Make CN ( Not open )
Dim strSQL As String = "INSERT INTO tblPersons (Name, Prename, E-Mail, Country) VALUES (@Name,@Prename,@E-Mail,@Country);"
'Specify SQL Here we will use "INSERT INTO "
Dim cm As New OleDbCommand(strSQL, cn)
'Create the command to do
cn.Open()
'open conncetion
cm.Parameters.AddWithValue("@Name", Name.Text)
cm.Parameters.AddWithValue("@Prename", Prename.Text)
cm.Parameters.AddWithValue("@E-Mail", EMail.Text)
cm.Parameters.AddWithValue("@Country", Country.Text)
' Set parameters we specifyd in the sql
cm.ExecuteNonQuery()
'excute the command
cn.Close()
'close connection
Name.Text = ""
Prename.Text = ""
EMail.Text = ""
Country.Text = ""
'Empty the textboxes
Button1.Visible = False
Button2.Visible = True
'make add buttons visible and hide the other
End Sub
Protected Sub Unnamed2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Button1.Visible = True
Button2.Visible = False
'make confirm buttons visible and hide the other
End Sub
Code: Select all
Here we are enter the parameters directly into the sql ( for string = use ' ' for integer just put it in.Protected Sub Unnamed1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("App_Data\dbPersons.mdb")
'Specify path of Db and Db Type
Dim cn As New OleDbConnection(strConn)
'Make CN ( Not open )
Dim strSQL As String = "INSERT INTO tblPersons (Name, Prename, E-Mail, Country) VALUES ('" & Name.Text & "','" & Pename.text & "','" & EMail.text & "','" & Country.text & '");"
'Specify SQL Here we will use "INSERT INTO "
Dim cm As New OleDbCommand(strSQL, cn)
'Create the command to do
cn.Open()
'open connection
cm.ExecuteNonQuery()
'excute the command
cn.Close()
'close connection
Name.Text = ""
Prename.Text = ""
EMail.Text = ""
Country.Text = ""
'Empty the textboxes
Button1.Visible = False
Button2.Visible = True
'make add buttons visible and hide the other
End Sub
Protected Sub Unnamed2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Button1.Visible = True
Button2.Visible = False
'make confirm buttons visible and hide the other
End Sub
Then u can try it ;)

New Database: Zip file: Rar file: Credits:
My teacher who taught me and my class.
You do not have the required permissions to view the files attached to this post.
Last edited by Kieken72 on Mon Mar 07, 2011 9:09 am, edited 2 times in total.
Nice tutorial.
I play around with ASPX and VWD put i always seem to have trouble using the database options.
Your tutorial did help me figure something out.
Although, I like using the Visual series of application "SDK's VWD is difficult to use. So seeing something like this is very helpful.
Scottie1972
I play around with ASPX and VWD put i always seem to have trouble using the database options.
Your tutorial did help me figure something out.
Although, I like using the Visual series of application "SDK's VWD is difficult to use. So seeing something like this is very helpful.
Scottie1972
Scottie1972 wrote:Nice tutorial.Thx for the comment,
I play around with ASPX and VWD put i always seem to have trouble using the database options.
Your tutorial did help me figure something out.
Although, I like using the Visual series of application "SDK's VWD is difficult to use. So seeing something like this is very helpful.
Scottie1972
and I'm working on the other SQL Features to "Edit" and to " Delete" Maybe i can finish them by tomorrow

Hi Kieken72
I've some troubles with the database. I put your code in my project but when i am debugging, I get always this problem:
![Image]()
This is my source code:
Thx
Sh4d0w009
I've some troubles with the database. I put your code in my project but when i am debugging, I get always this problem:

This is my source code:
Code: Select all
Can plz some1 help me out?Protected Sub Unnamed1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("App_Data\bib.mdb")
Dim cn As New OleDbConnection(strConn)
Dim strSQL As String = "INSERT INTO tblLeden (Voornaam, Naam, Adres, Gemeente, Postcode, Username, Password) VALUES (@Voornaam,@Naam,@Adres,@Gemeente,@Postcode,@Username,@Password);"
Dim cm As New OleDbCommand(strSQL, cn)
cn.Open()
cm.Parameters.AddWithValue("@Voornaam", txtvoornaam.Text)
cm.Parameters.AddWithValue("@Naam", txtnaam.Text)
cm.Parameters.AddWithValue("@Adres", txtadres.Text)
cm.Parameters.AddWithValue("@Postcode", txtgemeente.Text)
cm.Parameters.AddWithValue("@Gemeente", txtpostcode.Text)
cm.Parameters.AddWithValue("@Username", txtusername.Text)
cm.Parameters.AddWithValue("@Password", txtpassword1.Text)
cm.ExecuteNonQuery()
'Naam werd toegevoegd
cn.Close()
txtvoornaam.Text = ""
txtnaam.Text = ""
txtadres.Text = ""
txtgemeente.Text = ""
txtpostcode.Text = ""
txtusername.Text = ""
txtpassword1.Text = ""
txtpassword2.Text = ""
Button1.Visible = False
bevestig.Visible = True
End Sub
Protected Sub Unnamed2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Button1.Visible = True
bevestig.Visible = False
End Sub
Thx
Sh4d0w009
Edit:
For the one who download also download the new database and put the db into "App_Data"
For the one who download also download the new database and put the db into "App_Data"
6 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023