Enterprise Architect - Execute SQL Issue - sql

I'm getting the error "DAO.Database[3464] Data type mismatch in criteria expression." when attempting to update the t_connector table in Enterprise Architect by using the undocumented Execute command. Is what I'm trying to do not supported by EA's Execute SQL capabilities?
What I'm Doing
Note: This is within a loop so index is just an int. connector is an EA.Connector.
String addTrigger = "UPDATE t_connector SET PDATA1 = " + "'SAMPLE" + index + "'"
+ " WHERE Connector_ID = " + "'" + connector.ConnectorID + "';";
repository.Execute(addTrigger);
My Guess
I don't know SQL very well... did I mess up the statement somehow?
Further Information
A previous question I asked that lead me to what I'm doing now:
Add Trigger to Transition

Thanks #McAdam331 for your comment. Indeed, ConnectorID requires an int, not a string. Remove ' ' around connector.ConnectorID
String addTrigger = "UPDATE t_connector SET PDATA1 = " + "'SAMPLE" + index + "'"
+ " WHERE Connector_ID = " + connector.ConnectorID + ";";
repository.Execute(addTrigger);

Related

How to use sql command to update an sql table in vb.net?

I have the following SQL query which works in SQL server management :
Update SQLTableBlokke
set blokgemiddeld = ((tha_min4 + tha_min3 + tha_min2 +
tha_min1 + tha_huidig) /
NULLIF(((ABS(sign(tha_min4))+ABS(sign(tha_min3))+ABS(sign(tha_min2))+
ABS(sign(tha_min1))+ABS(sign(tha_huidig))) * 1.00),0))
As a beginner I have trouble using this command as a vb.net command. I have tried the following :
Dim konneksie As New SqlConnection
Dim opdraggem As New SqlCommand
konneksie.ConnectionString = "Data Source=GIDEON-E- LAPTOP\SQLEXPRESS2014;Initial Catalog=BlokwinsgewendheidDatabasis;Integrated Security=True"
konneksie.Open()
opdraggem.Connection = konneksie
opdraggem.CommandText = "Update(SQLTableBlokke)" & _
"blokgemiddeld = #((tha_min4 + tha_min3 + tha_min2 + tha_min1 + tha_huidig) / " & _
" NULLIF(((ABS(sign(tha_min4)) + ABS(sign(tha_min3)) + ABS(sign(tha_min2)) + ABS(sign(tha_min1)) + ABS(sign(tha_huidig))) * 1.0), 0)) "
opdraggem.ExecuteNonQuery()
However I get the error message : Additional information: Incorrect syntax near '('. The cursor stops at the opdraggem.ExecuteNonQuery() line.
I think I have to use parameters, but have no idea how to implement them.
Any help to a newbie would be much appreciated.
I don't see any need for you to use parameters in this case as you are not updating with any data not already in the table. Your CommandText should just be:
opdraggem.CommandText = "Update SQLTableBlokke" & _
"set blokgemiddeld = ((tha_min4 + tha_min3 + tha_min2 + " & _
"tha_min1 + tha_huidig) / " & _
"NULLIF(((ABS(sign(tha_min4))+ABS(sign(tha_min3))+ABS(sign(tha_min2))+ " & _
"ABS(sign(tha_min1))+ABS(sign(tha_huidig))) * 1.00),0))"
Your code will also benefit from a using statement on the connection as it will ensure the connection always gets closed. Demonstrated here:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx
Thank you all. Yes indeed I missed the keyword "Set".
Regards

VB6 SQL Type Mismatch issues with Databases

For my project, I have integrated a calculator which converts CM to FT. To make it worth more marks, I am trying to make it so that it will get the values and compare them against a Database to see which size Garage Door would be most suitable for the job
The SQL which purely searches for the value works. through and ADO which then only shows the found value on a data grid. But when I try to use the BETWEEN version, I get a Type Mismatch error which I have tried to fix by changing the variables to Integers and Reals, but it doesn't work. If anyone could help with this I would be really grateful!
Option Explicit
Dim sql As String
Dim sizeFindH As String
Dim sizeFindW As String
Dim sizeFindHUp As Double
Dim sizeFindHDown As Double
Dim sizeFindWUp As Double
Dim sizeFindWDown As Double
feet = 30.48
heightCm = txtHeightCm.Text
widthCm = txtWidthCm.Text
txtHeight.Text = heightCm / feet
txtWidth.Text = widthCm / feet
heightFt = txtHeight.Text
widthFt = txtWidth.Text
sizeFindH = txtHeight.Text
sizeFindW = txtWidth.Text
sizeFindHUp = sizeFindH + 1
sizeFindHDown = sizeFindH - 1
sizeFindWUp = sizeFindW + 1
sizeFindWDown = sizeFindW - 1
sql = "SELECT * FROM garageDoorSize WHERE ((garagedoorSize.height) BETWEEN '" + "%" + sizeFindHDown + "%" + "') AND '" + "%" + sizeFindHUp + "%" + "');"
sql = "SELECT * FROM garageDoorSize WHERE ((garagedoorSize.width) BETWEEN '" + "%" + sizeFindHDown + "%" + "') AND '" + "%" + sizeFindHUp + "%" + "');"
The error Type Mismatch highlights this line of code, as I'm sure it would the next if I could fix it.
sql = "SELECT * FROM garageDoorSize WHERE ((garagedoorSize.height) BETWEEN '" + "%" + sizeFindHDown + "%" + "') AND '" + "%" + sizeFindHUp + "%" + "');"
sql = "SELECT * FROM garageDoorSize WHERE ((garagedoorSize.width) BETWEEN '" + "%" + sizeFindHDown + "%" + "') AND '" + "%" + sizeFindHUp + "%" + "');"
The problem with your code in the SQL statement is that you are using an extra parentheses before the AND operator. It's not a logical operator here combining 2 conditions.. It's part of the BETWEEN clause and shouldn't have parentheses before. Also the % shouldn't be used here as others said.
Also the single quote is only used with strings.. So as long as your fields are numbers you shouldn't use it.
Oh.. And there's an extra parentheses without an opening one at the end of the query.
So your SQL should look like this:
sql = "SELECT * FROM garageDoorSize WHERE (garagedoorSize.width) BETWEEN " & sizeFindHDown & " AND " & sizeFindHUp & ";"
You are trying to concatenate a number and a string, hence the Type mismatch exception . Use CStr to convert the numbers to strings.
Also, I think you don't need the % signs, which are used with LIKE comparisons.
Also, as #jac correctly pointed out, use & to concatenate strings, not +. Edited my example to reflect for future readers of the post..
Try
sql = "SELECT * FROM garageDoorSize WHERE ((garagedoorSize.height) BETWEEN '" & CStr(sizeFindHDown) & "') AND '" & CStr(sizeFindHUp) & "');"
Note: Your code style of adding values direct to sql strings is vulnerable to sql injection (although not in this case as they are numbers). If these were strings, and came from user input your database and application is not secure. Best practice is to us sql parameter objects for your values... See this post, which shows usage. Note, first post I found, sure there are better guides out there :)

Syntax error in UPDATE statement vb 2010

i'm having trouble in my code. i just couldn't find the solution. When i run my program it always gives me an error
syntax error in UPDATE statement
here's the code.
Try
cmd.Connection = conn
cmd.CommandText = "UPDATE BC_Inventory SET [Price]='" + PriceU.Text + "',[Addition]='" + AddU.Text + "'," + _
"[Date_Updated]='" + DateU.Text + "',[Time_Updated]='" + TimeU.Text + "',[Updated_By]='" + UpdatedBy.Text + "'," + _
"WHERE [Item]='" + com_ItemU.Text + "'"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
conn.Close()
End Try
MsgBox("Inventory updated!")
You have added extra "," after [Updated_By] ([Updated_By]='" + UpdatedBy.Text + "'," <- extra ",") which is giving Syntax error
Please try this code
cmd.CommandText = "UPDATE BC_Inventory SET [Price]='" + PriceU.Text + "',[Addition]='" + AddU.Text + "'," + _
"[Date_Updated]='" + DateU.Text + "',[Time_Updated]='" + TimeU.Text + "',[Updated_By]='" + UpdatedBy.Text + "' " + _
"WHERE [Item]='" + com_ItemU.Text + "'"
Print your query out somehow before executing it, that's standard debugging 101.
That tends to solve 97.2% (a) of all SQL statement problems.
By that, I mean something like:
cmd.CommandText = "some hideously long and complex statement"
MessageBox (cmd.CommandText) ' Add this debug line temporarily'
cmd.ExecuteNonQuery()
In this particular case, it's the comma before the where clause.
(a) I just pulled this figure out of the ether. Actual results may vary, but I bet it'll fix more problems than not doing it :-)

"no key matching the described characteristics could be found within the current range"

I am using SQL CE 4.0 and the following code:
string sqlQuery = "SELECT TOP 10 " +
"tbl_Image.ImageID, tbl_Barcode.BarcodeValue, tbl_Image.ImageDateTime, tbl_Image.ImageFileName " +
"FROM " +
"tbl_Image " +
"JOIN tbl_Barcode on tbl_Image.ImageID = tbl_Barcode.ImageID " +
"WHERE " +
"tbl_Image.ImageID > #ImageID";
using (SqlCeCommand command = new SqlCeCommand(sqlQuery, sqlceServer.GetSqlConnection()))
{
command.Parameters.Add(new SqlCeParameter("#ImageID", SqlDbType.Int)).Value = imageID;
try
{
using (SqlCeDataReader rdr = command.ExecuteResultSet(ResultSetOptions.Scrollable))
{
if (rdr.HasRows)
{
while (rdr.Read())
{
But I keep getting the error :
"no key matching the described characteristics could be found within the current range"
on this line :
using (SqlCeDataReader rdr = command.ExecuteResultSet(ResultSetOptions.Scrollable))
I have tried changing this line to various different options to no avail.
My sql ce db is contains two tables created with the following (excerpt):
string tblImage =
"CREATE TABLE [tbl_Image] (" +
"[ImageID] int IDENTITY (1,1) NOT NULL" +
", [ImageData] image NOT NULL" +
", [ImageDateTime] datetime NOT NULL" +
", [ImageFileName] nvarchar(4000) NOT NULL" +
", [SettingID] int NOT NULL" +
", [MultiPage] bit NOT NULL" +
");";
string tblBarcode =
"CREATE TABLE [tbl_Barcode] (" +
"[BarcodeID] int IDENTITY (1,1) NOT NULL" +
", [ImageID] int NOT NULL" +
", [BarcodeType] nvarchar(4000) NOT NULL" +
", [BarcodeValue] nvarchar(4000) NOT NULL" +
");";
string alterImageTable =
"ALTER TABLE [tbl_Image] ADD CONSTRAINT [PK_tbl_Image] PRIMARY KEY ([ImageID]);";
string alterBarcodeTable =
"ALTER TABLE [tbl_Barcode] ADD CONSTRAINT [PK_tbl_Barcode] PRIMARY KEY ([BarcodeID]);";
I am puzzled as to what the problem is - googling the error seems to refer to replication issues which I am not doing. Can anyone help please?
EDIT:
Running the query in SQL Server Compact Toolbox v4.0 works no problem and returns the correct data.
Found a solution.
The top command must have the number in brackets ie TOP (10) and the HasRows command is not supported apparently so removing that line also enables the query to function.

Can you pass more than 1 parameter in a single SQL statement through a VB command?

Just wondering can you pass 2 parameters ( #Insertparanamehere ) in 1 sql command via VB? I have some code below ( sample code ) and I am just wondering is this possible.
Command = New SqlCommand("Update Boards Set CDF_Supplier_Tx='" + SupplierNameTxt.Text + "' Where CDF_Supplier_tx IN ( Select Supplier From Suppliers Where Supplier = '" + SupplierNameTxt.Text + "')", connection)
Where it says '" + SupplierNameTxt.Text + "' could this be potentially replaced with #Insertnameparaname from here?
If this is unclear as to which I will try to explain this a little more so my code would end up with 2x# instead of the long supplierNameTxt.Text?
This is just a question , thank you in advance.
MyCommand = New SqlCommand("UPDATE SeansMessage SET Message1 = #TicBoxText1, Message2 = #TicBoxText2 WHERE Number = 1", dbConn)
MyCommand.Parameters.AddWithValue("#TicBoxText1", TicBoxText1.Text)
MyCommand.Parameters.AddWithValue("#TicBoxText2", TicBoxText2.Text)
See here:
How to use parameters "#" in an SQL command in VB
You can also use the same parameter multiple times in your SQL text.