Friday, March 9, 2012

Problem when inserting to MS database

ok i've got several pages that insert with no problem but this one is giving me fits. The query it's generated works, because i've plugged it into the database through access and it worked. here is my code what am i doing wrong? It opens the connection and then gets to the executeNonQuery and doesn't do it not sure why.

Dim sqlInsert As String
Dim ssn As String
Dim ssnDash As String
Dim mma As String
Dim position As String
Dim subject As String
Dim mmaCheck As String
Dim subjectCheck As String
Dim bothCheck As String
Dim otherCheck As String
Dim flag As Boolean = False
Dim added As Boolean = False

Try
If txtSSN.Text <> "" Then
ssn = txtSSN.Text
ssnDash = ssn.Substring(0, 3) & "-" & ssn.Substring(3, 2) & "-" & ssn.Substring(5, 4)
End If

mma = dlMMA.SelectedValue.ToString
position = dlPosition.SelectedValue.ToString
subject = dlMajor.SelectedValue.ToString

Catch ex As Exception
Response.Write("Please fill in all fields")
End Try

otherCheck = mma & subject

Dim dr As DataRow

For Each dr In dsMajor.Tables("List_Of_MMA").Rows
If mma = dr.Item("mma").ToString And subject = dr.Item("Subject").ToString Then
mmaCheck = dr.Item("MMA").ToString
subjectCheck = dr.Item("Subject").ToString
flag = True
bothCheck = mmaCheck & subjectCheck
End If
Next

If flag = False Then
Response.Write("Didn't Work")
Else
sqlInsert = "INSERT INTO Student_Major(SSN, MMA, Subject, Position) Values('"
sqlInsert += ssnDash & "','"
sqlInsert += mmaCheck & "','"
sqlInsert += subjectCheck & "','"
sqlInsert += position & "')"

Dim com As OleDbCommand = New OleDbCommand(sqlInsert, myConnection)
Try
myConnection.Open()
com.ExecuteNonQuery()

added = True
Catch ex As Exception
Response.Write(sqlInsert)
Finally
myConnection.Close()
End Try

'If added = True Then
Response.Write("New Major, Minor, or Area added: " & subject)
'End If
End If

what is the xact error that you get ?|||i don't get an error it just will not execute the executeNonQuery command. I wish i got an error then i could fix it.|||Does it throw an exception? What's the message from exception handler??|||try doing a repsonse.write of your sqlInsert to see how its building up. also i'd recommend using parameterized queries..

hth|||Ok thanks for the replies we fixed the problem by changing the query to this

sqlInsert = "INSERT INTO Student_Major Values ('"
sqlInsert += ssnDash & "','"
sqlInsert += mma & "','"
sqlInsert += subject & "','"
sqlInsert += position & "')"

Where can i find information on parameterized queries?|||http://aspnet101.com/aspnet101/tutorials.aspx?id=1

No comments:

Post a Comment