jnordeng
asked on
Update Query Works In Access But Not From .NET
I have a situation here that is driving me nuts. First the yes/no columns answer to 0 and -1 not 0 and 1 like they do in .NET (checkboxes). I wrote a function to convert them but it shouldn't be like that.I have the UPDATE statement below that runs fine in access but from vb.net it fails with a syntax error. Enclosed is the UPDATE statement and the table schema. Any help is greatly appreciated. Thanks so much.
"UPDATE [XIFDAConfig] SET [Description] = '" & Me.txtXrayDescription.Text & "', CreatedBy = '" & Me.txtCreatedBy.Text & "', CreateDate = """ & Me.txtCreateDate.Text & _
""", BitDepth = " & Me.cmbBitDepth.Text & ", AutoExposure = " & Me.rbAutoExposure.Checked & ", [ImageType] = 0" & _
", Binning = " & Me.cmbBinning.Text & ", FullChip = " & Me.rbFullChip.Checked & ", [Left] = " & Me.numLeft.Value & _
", [Top] = " & Me.numTop.Value & ", [Width] = " & Me.numWidth.Value & ", [Heigth] = " & Me.numHeigth.Value & _
", Brightness = " & Me.numBrightness.Value & ", AutoGainLimit = " & Me.cmbAutoGain.Text & ", MinExposure = " & YN(Me.chkUseMinExposure) & _
", MinExposureValue = " & Me.numMinExposure.Value & ", ManualExposureValue = " & Me.numManualExposure.Value & ", ManualGainLimit = " & _
Me.numManualExposure.Value & ", BlackLevelSubtract = 0, ChipDefectCorrection = " & YN(Me.chkChipDefectCorrect ion) & _
", BackGroundSubtract= 0, NoiseFilter = " & YN(Me.chkNoiseFilter) & ", NoiseFilterValue = " & _
Me.numNoiseFilter.Value & ", BackGroundFrames = 0, BrightfieldImage = '" & Me.txtBrightfieldImage.Tex t & _
"', Voltage = " & Me.numVoltage.Value & ", Current = " & Me.numCurrent.Value & ", Maxma = " & YN(Me.chkMaxma) & _
", DefSysCal = " & YN(Me.chkUseSysCal) & ", SpatialCalFactor = '" & Me.txtCalibrationFactor.Te xt & "', ODCalFile = '" & Me.txtODCalFile.Text & _
"', [Default] = 0, AutoNormalize = " & YN(Me.chkAutomaticallyNorm alize) & ", DarkfieldImage = '" & Me.txtDarkfieldImage.Text & _
"' WHERE [ID] = " & IDValue & ";"
XIFDAConfig.zip
"UPDATE [XIFDAConfig] SET [Description] = '" & Me.txtXrayDescription.Text
""", BitDepth = " & Me.cmbBitDepth.Text & ", AutoExposure = " & Me.rbAutoExposure.Checked & ", [ImageType] = 0" & _
", Binning = " & Me.cmbBinning.Text & ", FullChip = " & Me.rbFullChip.Checked & ", [Left] = " & Me.numLeft.Value & _
", [Top] = " & Me.numTop.Value & ", [Width] = " & Me.numWidth.Value & ", [Heigth] = " & Me.numHeigth.Value & _
", Brightness = " & Me.numBrightness.Value & ", AutoGainLimit = " & Me.cmbAutoGain.Text & ", MinExposure = " & YN(Me.chkUseMinExposure) & _
", MinExposureValue = " & Me.numMinExposure.Value & ", ManualExposureValue = " & Me.numManualExposure.Value
Me.numManualExposure.Value
", BackGroundSubtract= 0, NoiseFilter = " & YN(Me.chkNoiseFilter) & ", NoiseFilterValue = " & _
Me.numNoiseFilter.Value & ", BackGroundFrames = 0, BrightfieldImage = '" & Me.txtBrightfieldImage.Tex
"', Voltage = " & Me.numVoltage.Value & ", Current = " & Me.numCurrent.Value & ", Maxma = " & YN(Me.chkMaxma) & _
", DefSysCal = " & YN(Me.chkUseSysCal) & ", SpatialCalFactor = '" & Me.txtCalibrationFactor.Te
"', [Default] = 0, AutoNormalize = " & YN(Me.chkAutomaticallyNorm
"' WHERE [ID] = " & IDValue & ";"
XIFDAConfig.zip
"Invalid Menu Handle" error when I download and try to open.
please post a sample value for the query above...
post code for YN(v) function
have a look at this
http://databases.aspfaq.com/database/how-do-i-handle-bit/boolean-columns.html
and make sure your YN(v) function returns "0": False/Ubchecked, "1":True/checked
http://databases.aspfaq.com/database/how-do-i-handle-bit/boolean-columns.html
and make sure your YN(v) function returns "0": False/Ubchecked, "1":True/checked
ASKER
Well the funciton YN is trained to return a value of -1 or 0 or the checkboxes instead of 0 and 1. That is what that does.
A lot of things could be wrong. But there is no way to tell in the way it is presented to us. A concatenation is not the result.
A syntax error can be a very simple ' missing somewhere. It is very hard to detect when you see the code. It would be a lot easier to pinpoint if you were looking at the resulting String instead of trying to read in between the numerous " & ". It could be the format of a date. Seeing the code does not tells us what the date looks like when it is passed to the database.
I freak when I see something such as CreateDate = """ & Me.txtCreateDate.Text & """. A user can type anything in a TextBox. For Access, 02-03-04 is not the same as 02/03/04. Is there some routine that checks if the date is valid before simply inserting the Text the user types into the SQL? Treating a Date as Text is never a good idea, since the format can change from one computer to another and be different than the one in the database. Is the date treated as a Date or as Text in the database? If it is as a date, then the date should be passed in the following format so as not to be dependant on the control panel of the caller:
CreateDate = #" & <Date in format MM-dd-yyyy> & "#"
Same thing with the TextBoxes that are supposed to return numerical values. Are they checked for a valid numeric value?
Where do you get the 1 from CheckBoxes in .NET? True is 1 in some languages and -1 in VB, but if you convert the result of a CheckBox that is True in VB.NET (CInt(CheckBox.Checked)), you do get -1. Anyway, VB sees everything that is zero as False and everything that is not zero as True. So 1 and -1 both gives you True. That should not be a problem. In any case, if you get 1 and need -1, simply do it this way, adding the minus sign:
MinExposure = -" & YN(Me.chkUseMinExposure)
Grab the result of the concatenation through a watch, a Debug.WriteLine or a MessageBox. Seeing it as it is, with the real values, enables you to see what the command really is. As a big concatenation, it is very hard to see what could be the problem. As a real SQL statement, a syntax error very often jumps at you.
A syntax error can be a very simple ' missing somewhere. It is very hard to detect when you see the code. It would be a lot easier to pinpoint if you were looking at the resulting String instead of trying to read in between the numerous " & ". It could be the format of a date. Seeing the code does not tells us what the date looks like when it is passed to the database.
I freak when I see something such as CreateDate = """ & Me.txtCreateDate.Text & """. A user can type anything in a TextBox. For Access, 02-03-04 is not the same as 02/03/04. Is there some routine that checks if the date is valid before simply inserting the Text the user types into the SQL? Treating a Date as Text is never a good idea, since the format can change from one computer to another and be different than the one in the database. Is the date treated as a Date or as Text in the database? If it is as a date, then the date should be passed in the following format so as not to be dependant on the control panel of the caller:
CreateDate = #" & <Date in format MM-dd-yyyy> & "#"
Same thing with the TextBoxes that are supposed to return numerical values. Are they checked for a valid numeric value?
Where do you get the 1 from CheckBoxes in .NET? True is 1 in some languages and -1 in VB, but if you convert the result of a CheckBox that is True in VB.NET (CInt(CheckBox.Checked)), you do get -1. Anyway, VB sees everything that is zero as False and everything that is not zero as True. So 1 and -1 both gives you True. That should not be a problem. In any case, if you get 1 and need -1, simply do it this way, adding the minus sign:
MinExposure = -" & YN(Me.chkUseMinExposure)
Grab the result of the concatenation through a watch, a Debug.WriteLine or a MessageBox. Seeing it as it is, with the real values, enables you to see what the command really is. As a big concatenation, it is very hard to see what could be the problem. As a real SQL statement, a syntax error very often jumps at you.
check with your single double quotes, normally access to .net understanding of these is different, to write a double quote in a string you can use " " " " in access where as in .net you have to use " " ", without spaces ofcourse.
ASKER
My problem is that the string it generates I copy and paste it in Access query and it runs fine. It makes the troubleshooting very difficult because I don't know why vb.net is telling me there is a syntax error. But once I get to work monday I will copy the output string to you.
ASKER
I have no trouble downloading the attached file from the original post by the way.
ASKER
The YN function is like this. Its an attempt to find out if this was causing the problem.
Function YN(ByVal chx As CheckBox) As Integer
If chx.Checked = True Then
Return 0
Else
Return -1
End If
End Function
Function YN(ByVal chx As CheckBox) As Integer
If chx.Checked = True Then
Return 0
Else
Return -1
End If
End Function
ASKER
This is the data call
Function DataAction(ByVal ds As DataSet, ByVal Table As DataTable, ByVal CommandText As String, ByVal ActionValue As SqlCommandType) As Boolean
Dim cmd As New OleDb.OleDbCommand(Command Text)
cmd.Connection = TableAdapter.Connection
Dim Conn As OleDb.OleDbConnection = cmd.Connection
Try
Dim da As New OleDb.OleDbDataAdapter
If cmd.Connection.State = ConnectionState.Closed Then
cmd.Connection.Open()
End If
Select Case ActionValue
Case 0
da.SelectCommand = cmd
ds.Tables(Table.TableName) .Clear()
da.Fill(ds.Tables(Table.Ta bleName))
Case 1
da.InsertCommand = cmd
cmd.ExecuteNonQuery()
'da.InsertCommand.ExecuteN onQuery()
Case 2
da.UpdateCommand = cmd
da.UpdateCommand.ExecuteNo nQuery()
Case 3
da.DeleteCommand = cmd
da.DeleteCommand.ExecuteNo nQuery()
End Select
cmd.Connection.Close()
Return True
Catch ex As Exception
ErrorString = ex.Message & vbCrLf & ex.ToString
Debug.WriteLine(cmd.Comman dText)
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
MessageBox.Show("An error has occured." & vbCrLf & vbCrLf & ErrorString)
Return False
End Try
End Function
Function DataAction(ByVal ds As DataSet, ByVal Table As DataTable, ByVal CommandText As String, ByVal ActionValue As SqlCommandType) As Boolean
Dim cmd As New OleDb.OleDbCommand(Command
cmd.Connection = TableAdapter.Connection
Dim Conn As OleDb.OleDbConnection = cmd.Connection
Try
Dim da As New OleDb.OleDbDataAdapter
If cmd.Connection.State = ConnectionState.Closed Then
cmd.Connection.Open()
End If
Select Case ActionValue
Case 0
da.SelectCommand = cmd
ds.Tables(Table.TableName)
da.Fill(ds.Tables(Table.Ta
Case 1
da.InsertCommand = cmd
cmd.ExecuteNonQuery()
'da.InsertCommand.ExecuteN
Case 2
da.UpdateCommand = cmd
da.UpdateCommand.ExecuteNo
Case 3
da.DeleteCommand = cmd
da.DeleteCommand.ExecuteNo
End Select
cmd.Connection.Close()
Return True
Catch ex As Exception
ErrorString = ex.Message & vbCrLf & ex.ToString
Debug.WriteLine(cmd.Comman
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
MessageBox.Show("An error has occured." & vbCrLf & vbCrLf & ErrorString)
Return False
End Try
End Function
ASKER
I took out all the line continuation too
If DataAction(Me.XIFDA, Me.XIFDA.Tables("XIFDAConf ig"), _
"UPDATE [XIFDAConfig] SET [Description] = '" & Me.txtXrayDescription.Text & "', CreatedBy = '" & Me.txtCreatedBy.Text & "', CreateDate = """ & Me.txtCreateDate.Text & """, BitDepth = " & Me.cmbBitDepth.Text & ", AutoExposure = " & RB(Me.rbAutoExposure) & ", [ImageType] = 0" & ", Binning = " & Me.cmbBinning.Text & ", FullChip = " & RB(Me.rbFullChip) & ", [Left] = " & Me.numLeft.Value & ", [Top] = " & Me.numTop.Value & ", [Width] = " & Me.numWidth.Value & ", [Heigth] = " & Me.numHeigth.Value & ", Brightness = " & Me.numBrightness.Value & ", AutoGainLimit = " & Me.cmbAutoGain.Text & ", MinExposure = " & CBool(Me.chkUseMinExposure .Checked) & ", MinExposureValue = " & Me.numMinExposure.Value & ", ManualExposureValue = " & Me.numManualExposure.Value & ", ManualGainLimit = " & Me.numManualExposure.Value & ", BlackLevelSubtract = 0, ChipDefectCorrection = " & CBool(Me.chkChipDefectCorr ection.Che cked) & ", BackGroundSubtract = 0, NoiseFilter = " & CBool(Me.chkNoiseFilter.Ch ecked) & ", NoiseFilterValue = " & Me.numNoiseFilter.Value & ", BackGroundFrames = 0, BrightfieldImage = '" & Me.txtBrightfieldImage.Tex t & "', Voltage = " & Me.numVoltage.Value & ", Current = " & Me.numCurrent.Value & ", Maxma = " & CBool(Me.chkMaxma.Checked) & ", DefSysCal = " & CBool(Me.chkUseSysCal.Chec ked) & ", SpatialCalFactor = '" & Me.txtCalibrationFactor.Te xt & "', ODCalFile = '" & Me.txtODCalFile.Text & "', [Default] = 0, AutoNormalize = " & CBool(Me.chkAutomaticallyN ormalize.C hecked) & ", DarkfieldImage = '" & Me.txtDarkfieldImage.Text & "' WHERE [ID] = " & IDValue & ";", SqlCommandType.UpdateData) = True Then
RefreshTables()
GetDGDataRow(IDValue, Me.txtXrayDescription.Text )
End If
If DataAction(Me.XIFDA, Me.XIFDA.Tables("XIFDAConf
"UPDATE [XIFDAConfig] SET [Description] = '" & Me.txtXrayDescription.Text
RefreshTables()
GetDGDataRow(IDValue, Me.txtXrayDescription.Text
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It was a stupid and dumb mistake on my part. I really don't care who gets credit.