ASPX & Database: SQL Add new Items
Posted: Sat Mar 05, 2011 3:50 pm
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.