473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ADD RECORDS

Using ASP.net 1.1
2 QUESTIONS:
1. Why do my write commands not work?
2. This file successfully makes changes in the database record. There
surely is some simple code I could add that would *add* records to this
database. Who can tell me?

<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Data.Oledb" %>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DBConnection As OledbConnection

DBConnection = New OledbConnection("Provider=Microsoft.Jet.Oledb.4.0; " & _

"Data Source=C:\Inetpub\wwwroot\app_data\Acc.mdb" )

DBConnection.Open()

Dim DBCommand As OledbCommand

DBCommand = New OledbCommand("SELECT * FROM table1, Acc")

Dim SQLString AS String

SQLString = "UPDATE Table1 SET TheEmpName='POOOOO' WHERE TheDate='6/14/07'"

DBCommand = New OleDBCommand(SQLString, DBConnection)

DBCommand.ExecuteNonquery()

Dim DBReader AS OledbDatareader

DBReader = DBCommand.ExecuteReader()

While DBReader.Read()

Response.Write(DBReader("TheDate"))

Response.Write(DBReader("TheEmpName"))

End while

' MyDataGrid.DataSource = DBReader

'MyDataGrid.DataBind()

DBReader.Close()

DBConnection.Close()

End Sub

</script>

</head>

<body>

<form id="form1" runat="server">

<asp:DataGrid id="MyDataGrid" runat="server"/>

</form>

</body>

</html>
Jul 13 '07 #1
14 1591
"dancer" <da****@microsoft.comwrote in message
news:uo*************@TK2MSFTNGP05.phx.gbl...
Using ASP.net 1.1
2 QUESTIONS:
1. Why do my write commands not work?
2. This file successfully makes changes in the database record. There
surely is some simple code I could add that would *add* records to this
database. Who can tell me?
Well, firstly if you're able to edit existing records, at least *some* of
your write commands are working... :-)

1) What code are you actually using to add records to the database? I see an
UPDATE SQL statement for updating existing records - where is your
corresponding INSERT statement for adding new records...?

2) What errors are you getting back from your database?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 14 '07 #2
RE:1) What code are you actually using to add records to the database? I
see an
UPDATE SQL statement for updating existing records - where is your
corresponding INSERT statement for adding new records...?
I am not using any code to add records. That was my question: What code can
I add to add records?

RE: 2) What errors are you getting back from your database?
None.
My code does exactly what it's intended to do: updates a record according to
a criteria. The only part that does not work are these two lines:

Response.Write(DBReader("TheDate"))

Response.Write(DBReader("TheEmpName"))

That's what I meant by "why does it not write?" I wanted to SEE if it
updates the record without having to look at the database, therefore I
included those lines, but they do not "write" so that I can see the updated
field.

Thank you.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eL**************@TK2MSFTNGP05.phx.gbl...
"dancer" <da****@microsoft.comwrote in message
news:uo*************@TK2MSFTNGP05.phx.gbl...
>Using ASP.net 1.1
2 QUESTIONS:
1. Why do my write commands not work?
2. This file successfully makes changes in the database record. There
surely is some simple code I could add that would *add* records to this
database. Who can tell me?

Well, firstly if you're able to edit existing records, at least *some* of
your write commands are working... :-)

1) What code are you actually using to add records to the database? I see
an UPDATE SQL statement for updating existing records - where is your
corresponding INSERT statement for adding new records...?

2) What errors are you getting back from your database?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 16 '07 #3
"dancer" <da****@microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.phx.gbl...
>RE:1) What code are you actually using to add records to the database?
I see an UPDATE SQL statement for updating existing records - where is
your corresponding INSERT statement for adding new records...?
I am not using any code to add records. That was my question: What code
can I add to add records?
Oh right...

The SQL syntax to add a record into a table in a Jet database is:

INSERT INTO Table (field1, Field2...) VALUES (Value1, Value2...)
RE: 2) What errors are you getting back from your database?
None.
My code does exactly what it's intended to do: updates a record according
to a criterion. The only part that does not work are these two lines:

Response.Write(DBReader("TheDate"))

Response.Write(DBReader("TheEmpName"))

That's what I meant by "why does it not write?" I wanted to SEE if it
updates the record without having to look at the database, therefore I
included those lines, but they do not "write" so that I can see the
updated field.
You're issuing your UPDATE statement correctly but, if you want to see that
it has worked, you will then need to issue a SELECT statement to fetch the
record you've just updated back out of the database...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 16 '07 #4
Thank you for helping me, Mark.

The INSERT INTO code works great as long as I'm inserting literals. For
example,
SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES(""Somebody"", ""10/10/98"", ""10/11/98"")"

But I need to insert variables received form an input form. When I put the
variable names in place of the literals like this:
SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES(EmpName, DateOfAccident, NotifyDate)"
I get this error message:
No value given for one or more required parameters

What is the syntax for using variables as the values?


"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:es*************@TK2MSFTNGP04.phx.gbl...
"dancer" <da****@microsoft.comwrote in message
news:em**************@TK2MSFTNGP04.phx.gbl...
>>RE:1) What code are you actually using to add records to the database?
I see an UPDATE SQL statement for updating existing records - where is
your corresponding INSERT statement for adding new records...?
I am not using any code to add records. That was my question: What code
can I add to add records?

Oh right...

The SQL syntax to add a record into a table in a Jet database is:

INSERT INTO Table (field1, Field2...) VALUES (Value1, Value2...)
>RE: 2) What errors are you getting back from your database?
None.
My code does exactly what it's intended to do: updates a record according
to a criterion. The only part that does not work are these two lines:

Response.Write(DBReader("TheDate"))

Response.Write(DBReader("TheEmpName"))

That's what I meant by "why does it not write?" I wanted to SEE if it
updates the record without having to look at the database, therefore I
included those lines, but they do not "write" so that I can see the
updated field.

You're issuing your UPDATE statement correctly but, if you want to see
that it has worked, you will then need to issue a SELECT statement to
fetch the record you've just updated back out of the database...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #5
"dancer" <da****@microsoft.comwrote in message
news:eC**************@TK2MSFTNGP02.phx.gbl...
Thank you for helping me, Mark.

The INSERT INTO code works great as long as I'm inserting literals. For
example,
SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES(""Somebody"", ""10/10/98"", ""10/11/98"")"

But I need to insert variables received form an input form. When I put
the variable names in place of the literals like this:
SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES(EmpName, DateOfAccident, NotifyDate)"
I get this error message:
No value given for one or more required parameters

What is the syntax for using variables as the values?
An SQL statement is a string like any other string, so you concatenate your
variables in the usual way:

SQLString = "INSERT INTO Table1(TheEmpName, TheDate, TheNotifyDate)VALUES('"
+ EmpName + "' etc...

However, this is an extremely dangerous practice, especially in a web
application, because of something called SQL Injection, but maybe that's
another story for another day...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #6
Hi Mark,

Can you tell me why I get a new record, but with nothing in it, even though
I get a form and fill it in and click submit?
Here's the code:
<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Data.Oledb" %>

<script language= "VB" runat="server">

'Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)

Sub btnSendDatabase_OnClick(Source As Object, E As EventArgs)
Dim DBConnection As OledbConnection

DBConnection = New OledbConnection("Provider=Microsoft.Jet.Oledb.4.0; " & _

"Data Source=C:\Inetpub\wwwroot\Acc.mdb" )

DBConnection.Open()

Dim DBCommand As OledbCommand

DBCommand = New OledbCommand("SELECT * FROM table1, Acc")

Dim SQLString AS String
Dim EmpName as String

Dim DateOfAccident as string

Dim NotifyDate as string
SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES('"+EmpName+"','"+DateOfAccide nt+"','"+NotifyDate+"')"

DBCommand = New OleDBCommand(SQLString, DBConnection)

DBCommand.ExecuteNonquery()
DBConnection.Close()

End Sub

</script>

</head>

<body>

<form id="form1" runat="server">

Employee's Name: <asp:textbox id="EmpName" runat=server columns="45"/>

<asp:textbox id="DateofAccident" runat=server /></asp:textbox>

<font face="Verdana" Size="2">Date Employer Notified <asp:textbox
id="Notifydate" runat=server/>

<asp:Button id="btnSendDatabase" text="Submit"
OnClick="btnSendDatabase_OnClick" runat="server" />
</form>

</body>

</html>





"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eL**************@TK2MSFTNGP02.phx.gbl...
"dancer" <da****@microsoft.comwrote in message
news:eC**************@TK2MSFTNGP02.phx.gbl...

Jul 17 '07 #7
"dancer" <da****@microsoft.comwrote in message
news:eZ**************@TK2MSFTNGP02.phx.gbl...
Can you tell me why I get a new record, but with nothing in it, even
though I get a form and fill it in and click submit?
A couple of things...

Firstly:
Dim DBCommand As OledbCommand
DBCommand = New OledbCommand("SELECT * FROM table1, Acc")
You're instantiating an OleDbCommand object, but never actually using it
before re-instantiating it further down in the code...
Secondly:
Dim SQLString AS String
Dim EmpName as String
Dim DateOfAccident as string
Dim NotifyDate as string

SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES('"+EmpName+"','"+DateOfAccide nt+"','"+NotifyDate+"')"
DBCommand = New OleDBCommand(SQLString, DBConnection)
DBCommand.ExecuteNonquery()
DBConnection.Close()
You're not actually populating the three data variables - therefore, you're
inserting a record into your table where all the fields are blank...

Dim EmpName as String = EmpName.Text
Dim DateOfAccident as string = DateOfAccident.Text
Dim NotifyDate as string = NotifyDate.Text
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #8
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Og**************@TK2MSFTNGP06.phx.gbl...
Dim EmpName as String = EmpName.Text
Dim DateOfAccident as string = DateOfAccident.Text
Dim NotifyDate as string = NotifyDate.Text
Actually, that might cause errors because the string variables have the same
names as the webcontrols...

Dim SQLString AS String
Dim strEmpName As String = EmpName.Text
Dim strDateOfAccident As String = DateOfAccident.Text
Dim strNotifyDate As String = NotifyDate.Text

SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES('"+ strEmpName+"','"+ strDateOfAccident+"','"+
strNotifyDate+"')"
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #9
ok It's working now. Thanks

Now - other question - I have 47 fields. Do I have to code '"+field1+"',
etc. 47 TIMES?!!
Is there not a way to say INSERT INTO Table 1 [all] VALUE [all] in some
form?
SURELY there is a way.

Also, I have seen
With Cmd Parameters
.Add(New OleDbParameter("@field1",
frmfield1.text))
etc.
What is the difference in that and INSERT?

Thank you.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Og**************@TK2MSFTNGP06.phx.gbl...
"dancer" <da****@microsoft.comwrote in message
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>Can you tell me why I get a new record, but with nothing in it, even
though I get a form and fill it in and click submit?

A couple of things...

Firstly:
>Dim DBCommand As OledbCommand
DBCommand = New OledbCommand("SELECT * FROM table1, Acc")

You're instantiating an OleDbCommand object, but never actually using it
before re-instantiating it further down in the code...
Secondly:
>Dim SQLString AS String
Dim EmpName as String
Dim DateOfAccident as string
Dim NotifyDate as string

SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES('"+EmpName+"','"+DateOfAccid ent+"','"+NotifyDate+"')"
DBCommand = New OleDBCommand(SQLString, DBConnection)
DBCommand.ExecuteNonquery()
DBConnection.Close()

You're not actually populating the three data variables - therefore,
you're inserting a record into your table where all the fields are
blank...

Dim EmpName as String = EmpName.Text
Dim DateOfAccident as string = DateOfAccident.Text
Dim NotifyDate as string = NotifyDate.Text
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #10
Yes, I got an error, so I put Dim TheEmpName as String = EmpName.Text, etc.,
which makes the names in the table the same as the variable names. It
works, but will it be a problem?

Thanks


"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Og**************@TK2MSFTNGP06.phx.gbl...
>Dim EmpName as String = EmpName.Text
Dim DateOfAccident as string = DateOfAccident.Text
Dim NotifyDate as string = NotifyDate.Text

Actually, that might cause errors because the string variables have the
same names as the webcontrols...

Dim SQLString AS String
Dim strEmpName As String = EmpName.Text
Dim strDateOfAccident As String = DateOfAccident.Text
Dim strNotifyDate As String = NotifyDate.Text

SQLString = "INSERT INTO Table1(TheEmpName, TheDate,
TheNotifyDate)VALUES('"+ strEmpName+"','"+ strDateOfAccident+"','"+
strNotifyDate+"')"
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #11
"dancer" <da****@microsoft.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Now - other question - I have 47 fields. Do I have to code '"+field1+"',
etc. 47 TIMES?!!
Yes.
Is there not a way to say INSERT INTO Table 1 [all] VALUE [all] in some
form?
No.
SURELY there is a way.
You could play about with generics and dictionaries and goodness knows what
but, at the end of the day, you can't really get away from the fact that
you've to send your database a piece of SQL, so you may as well just build
it...
Also, I have seen
With Cmd Parameters
.Add(New OleDbParameter("@field1",
frmfield1.text))
etc.
What is the difference in that and INSERT?
That's parameterisation - a much safer way of constructing database writes
which helps to eliminate SQL Injection. You would be well advised to adopt
this method.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #12
"dancer" <da****@microsoft.comwrote in message
news:uW**************@TK2MSFTNGP04.phx.gbl...
Yes, I got an error, so I put Dim TheEmpName as String = EmpName.Text,
etc., which makes the names in the table the same as the variable names.
It works, but will it be a problem?
No.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #13
RE: Cmd parameters Oh I would like to try that. But every time I try I get
all kinds of errors.
What does the @ mean? Do you have to declare @something or does the
compiler understand the @ as something special?
("@something" , frmsomething.text))
Could you define the above?

By injection you mean the user adding an ' in the data?

Thank you very much.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uU**************@TK2MSFTNGP05.phx.gbl...
"dancer" <da****@microsoft.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Now - other question - I have 47 fields. Do I have to code '"+field1+"',
etc. 47 TIMES?!!

Yes.
>Is there not a way to say INSERT INTO Table 1 [all] VALUE [all] in some
form?

No.
>SURELY there is a way.

You could play about with generics and dictionaries and goodness knows
what but, at the end of the day, you can't really get away from the fact
that you've to send your database a piece of SQL, so you may as well just
build it...
>Also, I have seen
With Cmd Parameters
.Add(New OleDbParameter("@field1",
frmfield1.text))
etc.
What is the difference in that and INSERT?

That's parameterisation - a much safer way of constructing database writes
which helps to eliminate SQL Injection. You would be well advised to adopt
this method.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #14
"dancer" <da****@microsoft.comwrote in message
news:O1**************@TK2MSFTNGP06.phx.gbl...
RE: Cmd parameters Oh I would like to try that. But every time I try I
get all kinds of errors.
http://www.java2s.com/Code/VB/Databa...dParameter.htm
By injection you mean the user adding an ' in the data?
No I don't - I mean this:
http://www.google.co.uk/search?sourc...L+Injection%22
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 17 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CSDunn | last post by:
Hello, I have an Access 2000 Project in which the data comes from a SQL Server 2000 database, and multiple users need to be able to see new records as each user adds records. The users also need...
1
by: Liz | last post by:
I have a table of about 10,000 records where each record has a numeric field named RecIdent. The value of RecIdent starts at 1 and is not sequential. For a given RecIdent, there may be only one...
6
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
5
by: Grant | last post by:
Hi Is there a way to recover deleted records from a table. A mass deletion has occurred and Access has been closed since it happened Louis
6
by: Damon Grieves | last post by:
Hi I just want to be sure I understand how the Access client works. If I have an Access back end with a million records on a server and an Access client. If the client is installed on the users pc...
13
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the...
1
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked pretty well up until I made the mistake of deleting...
4
by: LetMeDoIt | last post by:
Greetings, I'm using ASP to retrieve from MSSQL and I then populate a table. After several months of successfull retrieves, this same code now bombs out. It turns out that if I clear out from...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.