View Full Version : Adding a record to a table with 'INSERT INTO.....' and .aspx
reverendleo
05-24-2005, 05:21 PM
Hello All!
I'm having trouble with a form I've created with .aspx. The button I've created at the bottom of the form should take the values from the text boxes on the form, and add a new record to the table.
NewSQL = "INSERT INTO [Ref#vsJob#] (RefNum, JobName, [ExcavJob #], " & _
"PavJobNum, UtiJobNum) VALUES ('" & txtReferNum.text & "', '" & txtJobName.text & "', '" & txtExcavJobNum.text & "', " & _
"'" & txtPavJobNum.text & "', '" & txtUtilJobNum.text & "');"
cmdMbrs = New OleDbCommand( _
NewSQL, conClsf)
I know the connection string is good, because other parts of the form interact with the database correctly. I can't seem to figure out adding a record, though. Any ideas are SINCERELY appreciated!
Thanks, B. Drake
Freon22
05-24-2005, 06:51 PM
Strings should be enclosed in single quotes, and numbers should not.
reverendleo
05-24-2005, 08:37 PM
Hmm...no luck yet.
I double checked the table in Access, and all the fields except RefNum are text. When the Subroutine runs, by all perception it seems as if the code runs correctly, but upon opening Access and checking the table, no record has been written. I'm pretty sure the single quotes are correct below, but I'll keep working on it. Thanks for the help thusfar, and if anybody has other ideas, I'd love to hear them. Thanks!
conClsf = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
server.mappath("NOITracking.mdb") & ";")
conClsf.Open
NewSQL = "INSERT INTO [Ref#vsJob#] (RefNum, JobName, [ExcavJob #], " & _
"PavJobNum, UtiJobNum) VALUES (" & numRef & ", '" & txtJobName.text & _
"', '" & txtExcavJobNum.text & "', " & _
"'" & txtPavJobNum.text & "', '" & txtUtilJobNum.text & "');"
cmdMbrs = New OleDbCommand( _
NewSQL, conClsf)
cmdMbrs.Dispose
conClsf.Close
miranda
05-25-2005, 06:04 AM
are you working in VB or C#? Do you have the insert as a function? Here is a VB example
Sub Button1_Click(sender As Object, e As EventArgs)
If MyInsert(txtReferNum.text, txtJobName.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text) > 0 Then
'success
End If
End Sub
Function MyInsert(ByVal refNum As String, ByVal jobName As String, ByVal excavJob# As String, ByVal pavJobNum As String, ByVal utiJobNum As String) As Integer
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
server.mappath("NOITracking.mdb") & ";")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim SQL As String = "INSERT INTO [Ref#vsJob#] ([RefNum], [JobName], [ExcavJob#], [PavJobNum], [UtiJobNum]) VALUES (@RefNum, @JobName, @ExcavJob#, @PavJobNum, @UtiJobNum)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dbParam_refNum As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_refNum.ParameterName = "@RefNum"
dbParam_refNum.Value = refNum
dbParam_refNum.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_refNum)
Dim dbParam_jobName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_jobName.ParameterName = "@JobName"
dbParam_jobName.Value = jobName
dbParam_jobName.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_jobName)
Dim dbParam_excavJob# As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_excavJob#.ParameterName = "@ExcavJob#"
dbParam_excavJob#.Value = excavJob#
dbParam_excavJob#.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_excavJob#)
Dim dbParam_pavJobNum As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_pavJobNum.ParameterName = "@PavJobNum"
dbParam_pavJobNum.Value = pavJobNum
dbParam_pavJobNum.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_pavJobNum)
Dim dbParam_utiJobNum As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_utiJobNum.ParameterName = "@UtiJobNum"
dbParam_utiJobNum.Value = utiJobNum
dbParam_utiJobNum.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_utiJobNum)
Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try
Return rowsAffected
End Function
reverendleo
05-25-2005, 02:36 PM
I certainly appreciate the help, fellow Texan. Unfortunately, still no dice. By all appearance it's running exactly the same as before: looks like it might have worked, but really didn't do anything. I had to change some minor things (# to num) as it didn't like them while compiling, but, still no luck. I've attached the page as a .zip, so anyone who would like to take a look and offer any further suggestions would have my undying gratitude.
Thanks again! (.txt, that is)
miranda
05-25-2005, 04:19 PM
I see that refNum is a single so you need to make one more change. Also note that I added error handling and referenced a label control to show any errors or confirm the outcome. You will need to add the label control to your page if you uncomment the error handling.
Function MyInsert(ByVal RefNum As Single, ByVal JobName As String, ByVal ExcavJobNum As String, ByVal PavJobNum As String, ByVal UtiJobNum As String) As Integer
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("NOITracking.mdb") & ";"
Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
Dim SQL As String = "INSERT INTO [Ref#vsJob#] ([RefNum], [JobName], [ExcavJob #], [PavJobNum], [UtiJobNum]) " &_
"VALUES (" & RefNum & ",'" & JobName & "','" & ExcavJobNum & "','" & PavJobNum & "','" & UtiJobNum & "')"
Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = SQL
dbCommand.Connection = dbConnection
Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
'add a label to show the sql statement and the exception
'Label1.Text = SQL & "<br /><br />"
'Label1.Text += Exc.ToString()
Finally
dbConnection.Close
End Try
Return rowsAffected
End Function
Sub NewRec_Click(sender As Object, e As EventArgs)
If MyInsert(txtReferNum.text, txtJobName.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text) > 0 Then
'Label1.Text = "Success"
End If
End Sub
reverendleo
05-26-2005, 09:43 PM
Bless you for all your help thus far. Now I'm getting an error message "Identifier Expected" on line 173 (or possibly 175).
Line 173: Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
Line 174:
Line 175: Dim SQL As String = "INSERT INTO [tblJobs] ([RefNum], [JobName], [ExcavJob #], [PavJobNum], [UtiJobNum]) " &_
Any thoughts?
Thanks for the help!
miranda
05-28-2005, 04:22 PM
Is your database in the same directory as this file? If not then try this
1) hard code the path to the database instead of using server.MapPath. I do this in my web.config file and then all I need to do to reference it is to use ConfigurationSettings.AppSettings("myKey")
2) if you have added the label used for errors, uncomment the code in the catch block to display the sql statement and the exception. If the above didn't solve the problem, copy the entire error content of the label and post it here.
I hope that helps
reverendleo
05-31-2005, 02:29 PM
Hmm...still no luck
The database and .aspx page are in the same directory on a local server. Unfortunately, I'm still getting a Compliation Error 'Identifier Expected', so I'm not able to tell what's been generated in Label1. I've attached an updated .txt version of the file, if you'd like to take a look at it. Thank you SO much thusfar! I've tried several forums and you're the only one who seems to be able to help - much obliged, I am.
Fellow Texan,
B. Drake
miranda
05-31-2005, 04:15 PM
If you can zip it up and attach a copy of the database I will test it on my machine and see what I come up with. Sometimes I can see a problem that way that isn't appareant by looking at the code
reverendleo
05-31-2005, 04:31 PM
Bless you, Miranda.
miranda
05-31-2005, 06:07 PM
there is an error in the database that prevents me from opening it. I have attached a copy of the error
reverendleo
05-31-2005, 06:58 PM
Ah...
I had to remove a ton of stuff from the database to get it to compress to <50kb. The switchboard by default opens up, and it prevents users from messing with anything that they shouldn't. When you first open the database, it should prompt you with something like:
'This file may not be safe if it contains code.....'
To bypass this, you hold down 'shift' when you click 'Open'
Thanks again Miranda!
miranda
05-31-2005, 07:50 PM
Neither of these (Ref#vsJob# Ref#vsPermit#andNOT) is a Query or a table in the database you attached. That in itself will throw the error Identifier Expected because it is not finding a table nor is it finding an Access Query in the databse with those names.
miranda
06-01-2005, 03:00 PM
reverendleo, did you get your issue resolved?
reverendleo
06-01-2005, 04:01 PM
No dice yet...
I did change out all those tables that don't exist. I forgot I combined Ref#vsJob# and Ref#vsJob#andNOT into one table - tblJobs.
I switched all the locations, but am still getting the same error message as before. Here's an updated .txt file.
Thanks again Miranda, your the best!
Not sure if this will help, but check out this kb from MS http://support.microsoft.com/default.aspx?scid=kb;EN-US;q315990
Also, I don't see that you gave the form a name. All you have is <form runat="server">.
Now I don't know .NET so I'm sorry if I am off base here. Also, it's only my first post so I have a hundred more chances to screw up. :D
reverendleo
06-01-2005, 08:08 PM
Progress....sort of
Thanks for the tip. I'm not sure if adding the additional arguments to the <form> tag fixed it, but one thing that did is a simple space - " "
I missed a &_, should have been a & _
But now, I'm missing a required parameter somewhere in here
cmdMbrs = New OleDbCommand(strSQL1, conClsf)
rdrMbrs = cmdMbrs.ExecuteReader
New .txt file attached...I swear too, if we ever get this working I'm embedding your names in the code. Thanks again!
miranda
06-01-2005, 08:36 PM
I caught the missing space. &_ should have been & _. I have been trying to find the missing parameter and have made a few changes to your code. I will post it here for you shortly after i figure out what is missing.
Also, I don't see that you gave the form a name. All you have is <form runat="server">.
Jim no a form name is not needed.
I see this and no & before the _
cmdMbrs = New OleDbCommand( _
strSQL1, conClsf)
rdrMbrs = cmdMbrs.ExecuteReader
Also on
cmdMbrs = New OleDbCommand( _
strSQL2, conClsf)
rdrMbrs = cmdMbrs.ExecuteReader
Again, my disclaimer, I don't know .NET so I hope I am helping out here instead of hindering.
P.S. Thanks Miranda. I had no idea you could do that without a form name. I always thought it threw one in called default if you didn't name it.
miranda
06-01-2005, 09:09 PM
I see this and no & before the _
cmdMbrs = New OleDbCommand( _
strSQL1, conClsf)
rdrMbrs = cmdMbrs.ExecuteReader
Also on
cmdMbrs = New OleDbCommand( _
strSQL2, conClsf)
rdrMbrs = cmdMbrs.ExecuteReader
Again, my disclaimer, I don't know .NET so I hope I am helping out here instead of hindering.
P.S. Thanks Miranda. I had no idea you could do that without a form name. I always thought it threw one in called default if you didn't name it.
JimB the ampersand isn't needed in that instance.
reverendleo here you go this is working. there was a column name SWPPPBookYes/No that is not a column in your database table. Also I cleaned up the code and indented it to make it more legible.
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim conClsf As IDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("NOITracking.mdb") & ";")
Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Sub byJobNum_Click(sender As Object, e As EventArgs)
If (txtJobNum.text = "") Then
Exit Sub
Else
Dim strSQL as String = "SELECT RefNum, JobName, [ExcavJob #], PavJobNum, " & _
"UtiJobNum, PermitNum, FiledOnDate, PayTraceNum, [NOTYes/No], NOTDate " & _
"FROM tblJobs " & _
"WHERE [ExcavJob #] = '" & txtJobNum.text & "' OR " & _
"PavJobNum = '" & txtJobNum.text & "' OR " & _
"UtiJobNum = '" & txtJobNum.text & "' ORDER BY RefNum;"
conClsf.Open()
Dim cmdMbrs As OleDbCommand = New OleDbCommand(strSQL,conClsf)
Dim rdrMbrs As IDataReader = cmdMbrs.ExecuteReader(CommandBehavior.CloseConnection)
Try
if rdrMbrs.Read then
txtJobName.text = rdrMbrs.Item("JobName")
txtReferNum.text = rdrMbrs.Item("RefNum")
if ((rdrMbrs.Item("ExcavJob #")) is System.DBNull.Value) then
txtExcavJobNum.text = ""
else
txtExcavJobNum.text = rdrMbrs.Item("ExcavJob #")
end if
if ((rdrMbrs.Item("PavJobNum")) is System.DBNull.Value) then
txtPavJobNum.text = ""
else
txtPavJobNum.text = rdrMbrs.Item("PavJobNum")
end if
if ((rdrMbrs.Item("UtiJobNum")) is System.DBNull.Value) then
txtUtilJobNum.text = ""
else
txtUtilJobNum.text = rdrMbrs.Item("UtiJobNum")
end if
if ((rdrMbrs.Item("PermitNum")) is System.DBNull.Value) then
txtPerNum.text = ""
else
txtPerNum.text = rdrMbrs.Item("PermitNum")
end if
if ((rdrMbrs.Item("FiledOnDate")) is System.DBNull.Value) then
txtNOIDate.text = ""
else
txtNOIDate.text = rdrMbrs.Item("FiledOnDate")
end if
'chkNOTFiled.text = rdrMbrs.Item("NOTYes/No")
if ((rdrMbrs.Item("NOTDate")) is System.DBNull.Value) then
txtNOTDate.text = ""
else
txtNOTDate.text = rdrMbrs.Item("NOTDate")
end if
end if
Catch Exc as Exception
Label1.Text = strSQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
cmdMbrs.Dispose
conClsf.Close
txtJobNum.text = ""
txtRefNum.text = ""
End Try
End If
End Sub
Sub byRefNum_Click(sender As Object, e As EventArgs)
If (txtRefNum.text = "") Then
Exit Sub
Else
Dim strSQL As String = "SELECT RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum, " & _
"PermitNum, FiledOnDate, PayTraceNum, [NOTYes/No], NOTDate " & _
"FROM tblJobs " & _
"WHERE RefNum = " & txtRefNum.text & " ORDER BY RefNum;"
conClsf.Open()
Dim cmdMbrs As OleDbCommand = New OleDbCommand(strSQL,conClsf)
Dim rdrMbrs As IDataReader = cmdMbrs.ExecuteReader(CommandBehavior.CloseConnection)
Try
if rdrMbrs.Read then
txtJobName.text = rdrMbrs.Item("JobName")
txtReferNum.text = rdrMbrs.Item("RefNum")
if ((rdrMbrs.Item("ExcavJob #")) is System.DBNull.Value) then
txtExcavJobNum.text = ""
else
txtExcavJobNum.text = rdrMbrs.Item("ExcavJob #")
end if
if ((rdrMbrs.Item("PavJobNum")) is System.DBNull.Value) then
txtPavJobNum.text = ""
else
txtPavJobNum.text = rdrMbrs.Item("PavJobNum")
end if
if ((rdrMbrs.Item("UtiJobNum")) is System.DBNull.Value) then
txtUtilJobNum.text = ""
else
txtUtilJobNum.text = rdrMbrs.Item("UtiJobNum")
end if
if ((rdrMbrs.Item("PermitNum")) is System.DBNull.Value) then
txtPerNum.text = ""
else
txtPerNum.text = rdrMbrs.Item("PermitNum")
end if
if ((rdrMbrs.Item("FiledOnDate")) is System.DBNull.Value) then
txtNOIDate.text = ""
else
txtNOIDate.text = rdrMbrs.Item("FiledOnDate")
end if
'chkNOTFiled.text = rdrMbrs.Item("NOTYes/No")
if ((rdrMbrs.Item("NOTDate")) is System.DBNull.Value) then
txtNOTDate.text = ""
else
txtNOTDate.text = rdrMbrs.Item("NOTDate")
end if
end if
Catch Exc as Exception
Label1.Text = strSQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
cmdMbrs.Dispose
conClsf.Close
txtJobNum.text = ""
txtRefNum.text = ""
End Try
End If
End Sub
Function MyInsert(ByVal RefNum As Single, ByVal JobName As String, ByVal ExcavJobNum As String, ByVal PavJobNum As String, ByVal UtiJobNum As String) As Integer
Dim SQL As String = "INSERT INTO tblJobs (RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum) " & _
"VALUES (" & RefNum & ",'" & JobName & "','" & ExcavJobNum & "','" & PavJobNum & "','" & UtiJobNum & "')"
Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = SQL
dbCommand.Connection = conClsf
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
Sub NewRec_Click(sender As Object, e As EventArgs)
If MyInsert(txtReferNum.text, txtJobName.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text) > 0 Then
Label1.Text = "Success"
End If
End Sub
Sub SaveRec_Click(sender As Object, e As EventArgs)
Dim SaveSQL as String = ""
End Sub
Sub DeleteRec_Click(sender As Object, e As EventArgs)
Dim DeleteSQL as String = ""
End Sub
</script>
<html>
<head>
<title>NOI Tracking</title>
<style type="text/css">
.style1 {FONT-WEIGHT: bold; FONT-SIZE: x-large}
</style>
</head>
<body>
<form runat="server">
<table width="200" border="1">
<tbody>
<tr>
<td>
<div align="center"><span class="style1">NOI Tracking Page </span>
</div>
</td>
</tr>
<tr>
<td>
<table width="435" align="center" border="0">
<tbody>
<tr>
<td width="106">
<asp:TextBox id="txtJobNum" runat="server" Wrap="false" Width="50" TextMode="SingleLine" MaxLength="4"></asp:TextBox>
</td>
<td width="107">
<asp:Button id="SearchbyJobNum" onclick="byJobNum_Click" runat="server" Text="Search by Job Number"></asp:Button>
</td>
<td width="106">
<asp:TextBox id="txtRefNum" runat="server" Wrap="false" Width="50" TextMode="SingleLine" MaxLength="8"></asp:TextBox>
</td>
<td width="107">
<asp:Button id="SearchbyRefNum" onclick="byRefNum_Click" runat="server" Text="Search by Ref. Number"></asp:Button>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table width="592" align="center" border="0">
<tbody>
<tr>
<td width="586">
<asp:TextBox id="txtJobName" runat="server" Wrap="false" Width="300"></asp:TextBox>
Job Name
<asp:TextBox id="txtReferNum" runat="server" Wrap="false" Width="50" TextMode="SingleLine"></asp:TextBox>
Ref. #
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtExcavJobNum" runat="server" Wrap="false" Width="50" TextMode="SingleLine"></asp:TextBox>
Excav. Job #
<asp:TextBox id="txtPavJobNum" runat="server" Wrap="false" Width="50"></asp:TextBox>
Pav. Job #
<asp:TextBox id="txtUtilJobNum" runat="server" Wrap="false" Width="50"></asp:TextBox>
Util. Job #
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtPerNum" runat="server" Wrap="false" Width="180" TextMode="SingleLine"></asp:TextBox>
TXR Permit #
<asp:TextBox id="txtNOIDate" runat="server" Wrap="false" Width="100" TextMode="SingleLine"></asp:TextBox>
NOI Filed-On Date
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
<tr>
<td>
NOT Filed?
<asp:CheckBox id="chkNOTFiled" runat="server"></asp:CheckBox>
<asp:TextBox id="txtNOTDate" runat="server" Wrap="false" Width="100" TextMode="SingleLine"></asp:TextBox>
NOT Filed-On Date
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<asp:Button id="cmdNewRec" onclick="NewRec_Click" runat="server" Text="Create New Record"></asp:Button>
<asp:Button id="cmdSaveRec" onclick="SaveRec_Click" runat="server" Text="Save / Update Record"></asp:Button>
<asp:Button id="cmdDelRec" onclick="DeleteRec_Click" runat="server" Text="Delete Record"></asp:Button>
</td>
</tr>
</tbody>
</table>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
<p>
</p>
</form>
</body>
</html>
reverendleo
06-01-2005, 09:30 PM
SUCCESS!!!!!!!!
I'm putting your name in my code (with your permission, of course). Now, one last item: You saw the other two buttons which currently do nothing. Should I construct those the same way? (Add two additional MyInsert Functions with different SQL statements) As you noticed, one is for saving/updating the current record, and one for deleting the current record.
I knew a fellow Texan could come through. Thank you SO much!!!!
B. Drake
reverendleo
06-01-2005, 09:51 PM
Oh...one more thing.
There are three more fields from my table (which were already on the form). The field type for two of them is Date/Time. I added the ByVal.....as Date for those two fields.
Function MyInsert(ByVal RefNum As Single, ByVal JobName As String, ByVal ExcavJobNum As String, ByVal PavJobNum As String, ByVal UtiJobNum As String, ByVal PermitNum As String, ByVal NOIDate As Date, ByVal NOTDate As Date) As Integer
Dim SQL As String = "INSERT INTO tblJobs (RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum, PermitNum, FiledOnDate, NOTDate) " & _
"VALUES (" & RefNum & ",'" & JobName & "','" & ExcavJobNum & "','" & PavJobNum & "','" & UtiJobNum & "','" & PermitNum & "','" & NOIDate & "','" & NOTDate & "')"
When it calls the MyInsert Function...how do I need to change this to work properly (I'm getting a Data Type mismatch)
If MyInsert(txtReferNum.text, txtJobName.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text, txtPerNum.text, txtNOIDate.text, txtNOTDate.text) > 0 Then
miranda
06-01-2005, 10:25 PM
Replace those two with this. You may want to add a javascript confirm in the deletion to confirm the delete. That is always a good thing to do whenever you do a delete.
Sub SaveRec_Click(sender As Object, e As EventArgs)
If UpdateRec(txtReferNum.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text, txtJobName.text, txtPerNum.text, txtNOIDate.text, chkNOTFiled.Checked, txtNOTDate.text) > 0 Then
Label1.Text = txtRefNum.text & " has been updated"
txtRefNum.text = ""
End If
End Sub
Function UpdateRec(ByVal refNum As Integer, ByVal excavJobNum As String, ByVal pavJobNum As String, ByVal utiJobNum As String, ByVal jobName As String, ByVal permitNum As String, ByVal filedOnDate As Date, ByVal nOTYes/No As Boolean, ByVal nOTDate As Date) As Integer
Dim SQL As String = "UPDATE tblJobs SET FiledOnDate = '" & FiledOnDate & "', UtiJobNum = '" & UtiJobNum & "', " & _
"NOTDate = '" & NOTDate & "', [NOTYes/No] = '" NOTYesNo & "', JobName = '" & JobName & "', "& _
"PavJobNum = '" & PavJobNum & "', PermitNum = '" & PermitNum & "', [ExcavJob #] = '" & ExcavJobNum & "'"& _
"WHERE (RefNum = " & RefNum & ")"
Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = SQL
dbCommand.Connection = conClsf
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
Sub DeleteRec_Click(sender As Object, e As EventArgs)
If DeleteRec(txtRefNum.text) > 0 Then
Label1.Text = txtRefNum.text & " has been deleted"
txtRefNum.text = ""
End If
End Sub
Function DeleteRec(ByVal refNum As Integer) As Integer
Dim SQL As String = "DELETE FROM tblJobs WHERE (RefNum = " & RefNum & ")"
Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = SQL
dbCommand.Connection = conClsf
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
reverendleo
06-02-2005, 08:54 PM
Things are looking on the up and up!
Still having a bit of an issue with a Data Type Mismatch
Here's what's being generated in the handy Label1 when the NewRecord Button is clicked:
INSERT INTO tblJobs (RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum, PermitNum, FiledOnDate, [NOTYes/No], NOTDate) VALUES (101,'Bardin Road Contract B','','3805','','TXR15O987','4/27/2003','True','8/30/2004');
System.Data.OleDb.OleDbException: Data type mismatch in criteria expression. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling
(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResul (tagDBPARAMS dbParams, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand
(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String
method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at ASP.NOITracking_aspx.MyInsert(Single RefNum, String JobName, String
ExcavJobNum, String PavJobNum, String UtiJobNum, String PermitNum, DateTime NOIDate, Boolean NOTYesNo, DateTime NOTDate) in D:\IntraNet\NOITracking\NOITracking.aspx:line 154
Something to do with one of the 'Date' types, or possibly the 'Boolean'
Thanks again, you will forever have my unrelenting gratitude
miranda
06-03-2005, 06:35 AM
oops, I missed that NotYes/No was a boolean and therefore does not have single quotes around it. Just for grins I made a few more changes for you. (like adding a javascript confirm on the delete, this can be found in the Page_Load event)
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim conClsf As IDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("NOITrackingCopy.mdb") & ";")
Sub Page_Load(sender As Object, e As EventArgs)
cmdDelRec.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")
End Sub
Sub byJobNum_Click(sender As Object, e As EventArgs)
If (txtJobNum.text = "") Then
Exit Sub
Else
Dim strSQL as String = "SELECT RefNum, JobName, [ExcavJob #], PavJobNum, " & _
"UtiJobNum, PermitNum, FiledOnDate, PayTraceNum, [NOTYes/No], NOTDate " & _
"FROM tblJobs " & _
"WHERE [ExcavJob #] = '" & txtJobNum.text & "' OR " & _
"PavJobNum = '" & txtJobNum.text & "' OR " & _
"UtiJobNum = '" & txtJobNum.text & "' ORDER BY RefNum;"
conClsf.Open()
Dim cmdMbrs As OleDbCommand = New OleDbCommand(strSQL,conClsf)
Dim rdrMbrs As IDataReader = cmdMbrs.ExecuteReader(CommandBehavior.CloseConnection)
Try
if rdrMbrs.Read then
txtJobName.text = rdrMbrs.Item("JobName")
txtReferNum.text = rdrMbrs.Item("RefNum")
if ((rdrMbrs.Item("ExcavJob #")) is System.DBNull.Value) then
txtExcavJobNum.text = ""
else
txtExcavJobNum.text = rdrMbrs.Item("ExcavJob #")
end if
if ((rdrMbrs.Item("PavJobNum")) is System.DBNull.Value) then
txtPavJobNum.text = ""
else
txtPavJobNum.text = rdrMbrs.Item("PavJobNum")
end if
if ((rdrMbrs.Item("UtiJobNum")) is System.DBNull.Value) then
txtUtilJobNum.text = ""
else
txtUtilJobNum.text = rdrMbrs.Item("UtiJobNum")
end if
if ((rdrMbrs.Item("PermitNum")) is System.DBNull.Value) then
txtPerNum.text = ""
else
txtPerNum.text = rdrMbrs.Item("PermitNum")
end if
if ((rdrMbrs.Item("FiledOnDate")) is System.DBNull.Value) then
txtNOIDate.text = ""
else
txtNOIDate.text = rdrMbrs.Item("FiledOnDate")
end if
If rdrMbrs.Item("NOTYes/No") = -1 Then chkNOTFiled.Checked = True
if ((rdrMbrs.Item("NOTDate")) is System.DBNull.Value) then
txtNOTDate.text = ""
else
txtNOTDate.text = rdrMbrs.Item("NOTDate")
end if
else
Label1.Text = txtJobNum.text & " Was Not Found"
end if
Catch Exc as Exception
Label1.Text = strSQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
cmdMbrs.Dispose
conClsf.Close
txtJobNum.text = ""
txtRefNum.text = ""
cmdNewRec.Enabled = True
cmdSaveRec.Enabled = True
cmdDelRec.Enabled = True
End Try
End If
End Sub
Sub byRefNum_Click(sender As Object, e As EventArgs)
If (txtRefNum.text = "") Then
Exit Sub
Else
byRefNum(txtRefNum.text)
End If
cmdNewRec.Enabled = True
cmdSaveRec.Enabled = True
cmdDelRec.Enabled = True
End Sub
Sub byRefNum(ByVal RefNum As Single)
Dim strSQL As String = "SELECT RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum, " & _
"PermitNum, FiledOnDate, PayTraceNum, [NOTYes/No], NOTDate " & _
"FROM tblJobs " & _
"WHERE RefNum = " & RefNum & " ORDER BY RefNum;"
conClsf.Open()
Dim cmdMbrs As OleDbCommand = New OleDbCommand(strSQL,conClsf)
Dim rdrMbrs As IDataReader = cmdMbrs.ExecuteReader(CommandBehavior.CloseConnection)
Try
if rdrMbrs.Read then
txtJobName.text = rdrMbrs.Item("JobName")
txtReferNum.text = rdrMbrs.Item("RefNum")
if ((rdrMbrs.Item("ExcavJob #")) is System.DBNull.Value) then
txtExcavJobNum.text = ""
else
txtExcavJobNum.text = rdrMbrs.Item("ExcavJob #")
end if
if ((rdrMbrs.Item("PavJobNum")) is System.DBNull.Value) then
txtPavJobNum.text = ""
else
txtPavJobNum.text = rdrMbrs.Item("PavJobNum")
end if
if ((rdrMbrs.Item("UtiJobNum")) is System.DBNull.Value) then
txtUtilJobNum.text = ""
else
txtUtilJobNum.text = rdrMbrs.Item("UtiJobNum")
end if
if ((rdrMbrs.Item("PermitNum")) is System.DBNull.Value) then
txtPerNum.text = ""
else
txtPerNum.text = rdrMbrs.Item("PermitNum")
end if
if ((rdrMbrs.Item("FiledOnDate")) is System.DBNull.Value) then
txtNOIDate.text = ""
else
txtNOIDate.text = rdrMbrs.Item("FiledOnDate")
end if
If rdrMbrs.Item("NOTYes/No") = -1 Then chkNOTFiled.Checked = True
if ((rdrMbrs.Item("NOTDate")) is System.DBNull.Value) then
txtNOTDate.text = ""
else
txtNOTDate.text = rdrMbrs.Item("NOTDate")
end if
else
Label1.Text = RefNum & " Was Not Found"
end if
Catch Exc as Exception
Label1.Text = strSQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
cmdMbrs.Dispose
conClsf.Close
txtJobNum.text = ""
txtRefNum.text = ""
End Try
End Sub
Function MyInsert(ByVal RefNum As Single, ByVal JobName As String, ByVal ExcavJobNum As String, ByVal PavJobNum As String, ByVal UtiJobNum As String) As Integer
Dim SQL As String = "INSERT INTO tblJobs (RefNum, JobName, [ExcavJob #], PavJobNum, UtiJobNum) " & _
"VALUES (" & RefNum & ",'" & JobName & "','" & ExcavJobNum & "','" & PavJobNum & "','" & UtiJobNum & "')"
Dim dbCommand As IDbCommand = New OleDbCommand(SQL, conClsf)
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
Sub NewRec_Click(sender As Object, e As EventArgs)
If MyInsert(txtReferNum.text, txtJobName.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text) > 0 Then
Label1.Text = "Success"
End If
End Sub
Sub SaveRec_Click(sender As Object, e As EventArgs)
If UpdateRec(txtReferNum.text, txtExcavJobNum.text, txtPavJobNum.text, txtUtilJobNum.text, txtJobName.text, txtPerNum.text, txtNOIDate.text, chkNOTFiled.Checked, txtNOTDate.text) > 0 Then
Label1.Text = txtReferNum.text & " has been updated"
byRefNum(txtReferNum.text)
Else
Label1.Text = txtReferNum.text & " has NOT been updated"
End If
End Sub
Function UpdateRec(ByVal refNum As Integer, ByVal excavJobNum As String, ByVal pavJobNum As String, ByVal utiJobNum As String, ByVal jobName As String, ByVal permitNum As String, ByVal filedOnDate As Date, ByVal nOTYesNo As Boolean, ByVal nOTDate As Date) As Integer
Dim SQL As String = "UPDATE tblJobs SET FiledOnDate = '" & FiledOnDate & "', UtiJobNum = '" & UtiJobNum & "', " & _
"NOTDate = '" & NOTDate & "', [NOTYes/No] = " & NOTYesNo & ", JobName = '" & JobName & "', "& _
"PavJobNum = '" & PavJobNum & "', PermitNum = '" & PermitNum & "', [ExcavJob #] = '" & ExcavJobNum & "'"& _
"WHERE (RefNum = " & refNum & ")"
Dim dbCommand As IDbCommand = New OleDbCommand(SQL, conClsf)
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
Sub DeleteRec_Click(sender As Object, e As EventArgs)
If DeleteRec(txtReferNum.text) > 0 Then
Label1.Text = txtReferNum.text & " has been deleted"
txtRefNum.text = ""
End If
End Sub
Function DeleteRec(ByVal refNum As Integer) As Integer
Dim SQL As String = "DELETE FROM tblJobs WHERE (RefNum = " & RefNum & ")"
Dim dbCommand As IDbCommand = New OleDbCommand(SQL, conClsf)
Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try
Return rowsAffected
End Function
</script>
<html>
<head>
<title>NOI Tracking</title> <style type="text/css">.style1 {
FONT-WEIGHT: bold; FONT-SIZE: x-large
}
</style>
</head>
<body>
<form runat="server">
<table width="200" border="1">
<tbody>
<tr>
<td>
<div align="center"><span class="style1">NOI Tracking Page </span>
</div>
</td>
</tr>
<tr>
<td>
<table width="435" align="center" border="0">
<tbody>
<tr>
<td width="106">
<asp:TextBox id="txtJobNum" runat="server" MaxLength="4" TextMode="SingleLine" Width="50" Wrap="false"></asp:TextBox>
</td>
<td width="107">
<asp:Button id="SearchbyJobNum" onclick="byJobNum_Click" runat="server" Text="Search by Job Number"></asp:Button>
</td>
<td width="106">
<asp:TextBox id="txtRefNum" runat="server" MaxLength="8" TextMode="SingleLine" Width="50" Wrap="false"></asp:TextBox>
</td>
<td width="107">
<asp:Button id="SearchbyRefNum" onclick="byRefNum_Click" runat="server" Text="Search by Ref. Number"></asp:Button>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table width="592" align="center" border="0">
<tbody>
<tr>
<td width="586">
<asp:TextBox id="txtJobName" runat="server" Width="300" Wrap="false"></asp:TextBox>
Job Name
<asp:TextBox id="txtReferNum" runat="server" TextMode="SingleLine" Width="50" Wrap="false"></asp:TextBox>
Ref. #
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtExcavJobNum" runat="server" TextMode="SingleLine" Width="50" Wrap="false"></asp:TextBox>
Excav. Job #
<asp:TextBox id="txtPavJobNum" runat="server" Width="50" Wrap="false"></asp:TextBox>
Pav. Job #
<asp:TextBox id="txtUtilJobNum" runat="server" Width="50" Wrap="false"></asp:TextBox>
Util. Job #
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtPerNum" runat="server" TextMode="SingleLine" Width="180" Wrap="false"></asp:TextBox>
TXR Permit #
<asp:TextBox id="txtNOIDate" runat="server" TextMode="SingleLine" Width="100" Wrap="false"></asp:TextBox>
NOI Filed-On Date
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
<tr>
<td>
NOT Filed?
<asp:CheckBox id="chkNOTFiled" runat="server"></asp:CheckBox>
<asp:TextBox id="txtNOTDate" runat="server" TextMode="SingleLine" Width="100" Wrap="false"></asp:TextBox>
NOT Filed-On Date
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<asp:Button id="cmdNewRec" onclick="NewRec_Click" runat="server" Text="Create New Record" Enabled="False"></asp:Button>
<asp:Button id="cmdSaveRec" onclick="SaveRec_Click" runat="server" Text="Save / Update Record" Enabled="False"></asp:Button>
<asp:Button id="cmdDelRec" onclick="DeleteRec_Click" runat="server" Text="Delete Record" Enabled="False"></asp:Button>
</td>
</tr>
</tbody>
</table>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
<p>
</p>
</form>
</body>
</html>
reverendleo
06-03-2005, 02:50 PM
Nice! You've really outdone yourself, and I hope you are making a healthy salary doing this, because you should. I added the date and boolean fields to the MyInsert function, and now I think we're down to one last issue. I'm getting the following error (a server error) that says the following:
System.InvalidCastException: Cast from string "" to type 'Date' is not valid.
I believe it doesn't like sending an empty string to the date field. So - what's the best way to tackle that? Should I add an If-Then-Else that modifies the SQL statement based on which fields have text in them? For Example - if a user wants to enter a new record that has an NOI Date, but no NOT date, use a SQL statement that does not include the NOTDate field. Seems like there'd be a more efficient way to handle that. Any thoughts?
You're the greatest!
miranda
06-03-2005, 06:40 PM
I have attached the newest version for you. It is kinda ugly but solves the problem.
Nice! You've really outdone yourself, and I hope you are making a healthy salary doing this, because you should.
I do freelance work, and am open to new jobs.
reverendleo
06-03-2005, 08:12 PM
AT LAST, IT IS DONE!!!!! IT WORKS, EVERYTHING WORKS!!!!!
Miranda, I took what you sent back and tweeked it a little, cutting out a few things, and now it works exactly like I need it to. There should never be a record submitted w/o an NOI Date, so I just copied the MyInsert and UpdateRec functions, renamed them, and took out the argument for NOTDate. The code checks to see if txtNOTDate is "", and executes the new functions in that case. Otherwise, it executes the original MyInsert and UpdateRec.
I added a few cosmetic things, and a 'Clear Fields' button, but it's all there, and all works.
I can't thank you enough for your help, you're a real miracle worker. I've attached the new .txt file if you're interested in seeing the finished product.
Thanks and thanks again!!!!! :thumbsup:
miranda
06-03-2005, 08:26 PM
You are welcome. I am glad that I could help. At times I may be in the middle of a long project in another language or technology and this is how I keep my coding skills tuned.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.