473,321 Members | 1,669 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,321 software developers and data experts.

The Error of My Ways

I was hoping for a little code review. This is my first real .NET
application, and would appreciate some advice. For example, in the code
below, notice that I call cn.CreateCommand twice. I tried re-using the
original object and just resetting it's CommandText property, but I got
unreadable output in the results.

The code below creates a "parent" SQL Server record and get's its Identity
value for subsequent "child" inserts.

Although the code works, and is actually quite fast, the code just doesn't
look elegant. Suggestions?

Note, this is just a subset of all the code. I do close the global SQL
connection at the end of processing.

thx

Jeff
Public Class Form1
Inherits System.Windows.Forms.Form

Dim cn As New SqlClient.SqlConnection
....
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
cn.ConnectionString = sConnect
cn.Open()
End Sub
Private Function WriteToDB() As Boolean
Dim myDataAdapter As New SqlClient.SqlDataAdapter
Dim myDataSet As New System.Data.DataSet
Dim myTable As New System.Data.DataTable
Dim dr As System.Data.DataRow
Dim sNewIncidentID As String
Dim sNewExposureID As String
Dim sNewBasicID As String
Dim sNewApparatusID As String
Dim sSQL As String
Dim sAccountID As String = "9"
Dim sState As String = "CA"

Dim cmd As SqlClient.SqlCommand = cn.CreateCommand

sSQL = "exec ers_CreateIncident " & _
sAccountID & _
",'" & sState & "'" & _
",'" & sRECEIVED_DATE & "'" & _
",'" & sRECEIVED_TIME & "'" & _
",'" & sIncidentAddress & "<BR>" & sState & "'" & _
",'" & sIncidentType & "'"

cmd.CommandText = sSQL

myDataAdapter.SelectCommand = cmd

myDataAdapter.Fill(myDataSet)

myTable = myDataSet.Tables(0)

If myTable.Rows.Count > 0 Then
dr = myTable.Rows(0)
sNewIncidentID = dr("NewIncidentID")
cmd = cn.CreateCommand
cmd.CommandText = "exec ers_CreateExposure " & sNewIncidentID
myDataAdapter = New SqlClient.SqlDataAdapter
myDataAdapter.SelectCommand = cmd
myDataSet = New System.Data.DataSet
myDataAdapter.Fill(myDataSet)
myTable = myDataSet.Tables(0)
End If

End Function

End Class
The Stored Procedure:

create procedure ers_CreateIncident
@AID int,
@State varchar(10),
@IncidentDate varchar(8),
@IncidentTime varchar(6),
@Address varchar(500),
@IncidentType varchar(20)
as

declare @IncidentNumber int
declare @FDID varchar(5)

select @IncidentNumber = isnull(max(convert(int, incidentnumber)), 0) + 1
from incidents
where aid = @AID
and DATEPART(yy, IncidentDate) = DATEPART(yy, GetDate())

select @FDID = FDID from Accounts
where AccountID = @AID

INSERT Incidents (AID,
State,
FDID,
IncidentDate,
IncidentTime,
IncidentNumber,
Address,
IncidentType)
values(
@AID,
@State,
@FDID,
@IncidentDate,
@IncidentTime,
@IncidentNumber,
@Address,
@IncidentType)
SELECT SCOPE_IDENTITY() as NewIncidentID


Nov 21 '05 #1
0 844

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

Similar topics

5
by: Csaba Gabor | last post by:
The usual example shown for trapping for failure of COM instantiation (Windows systems) is something like (see for example http://at2.php.net/manual/en/class.com.php): $word = new...
5
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
3
by: Javier Camacho | last post by:
Hi, We've a computer ( in our develepment group) that has installed Visual Studio.NET 2003 and Delphi 5 and Delphi 7 . When the user try to connect to database in the SQL Server (installed on...
3
by: Alejandro Penate-Diaz | last post by:
Hi. While trying to create a a word document in one of my asp.net pages I keep getting an ASP.NET permission error. first I thought it was been caused while trying to save the document, and I added...
3
by: Frustrated Developer via DotNetMonster.com | last post by:
I have developed a form that would allow the user to load and search a database several ways, by data range, by specific number or all database entries. I can successfully load any of the ways...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
34
by: Rory Campbell-Lange | last post by:
>>(1.0/10.0) + (2.0/10.0) + (3.0/10.0) 0.60000000000000009 0.59999999999999998 Is using the decimal module the best way around this? (I'm expecting the first sum to match the second). It seem...
4
by: Ravi, Dallas, Texas | last post by:
http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic22503.aspx shows ways to get errors in a server context. i am trying to find ways to trap the line number where...
9
by: Brian Dude | last post by:
Hello, I'm really at a loss as to what's going wrong with my code. I have it posted below. How it appears is how I had it originally. I've deleted all error-checking for simplicity: #include...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.