How would I set following into array:
Public Function opcijeMp(ByVal hwid As String)
Dim hardware As String = hwid
Dim result = New List(Of String)()
Try
ManageConnection(False, konekcija) 'Open connection'
Dim strQuery As String = "SELECT * FROM info.opcije_mp as mp inner join instalacije as i where mp.idopcije_mp =
i.opcijeMP and i.instalacije_hwid = '" + Globals.cpuid + "';"
Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()
While reader.Read()
Globals.prodaja = reader.GetString("Prodaja")
Globals.Kalkulacije = reader.GetString("Kalkulacije")
Globals.Zaduznice = reader.GetString("Zaduznice")
Globals.Predisponacije = reader.GetString("Predisponacije")
Globals.Robno = reader.GetString("Robno")
Globals.KUF = reader.GetString("KUF")
Globals.KIF = reader.GetString("KIF")
Globals.Narudzbenice = reader.GetString("Narudzbenice")
Globals.Nalozi = reader.GetString("Nalozi")
Globals.akcijskeCijene = reader.GetString("Akcijske_cijene")
Globals.servisnaRoba = reader.GetString("Servisna_roba")
Globals.Ostalo1 = reader.GetString("Ostalo1")
Globals.Ostalo2 = reader.GetString("Ostalo2")
Globals.Ostalo3 = reader.GetString("Ostalo3")
End While
reader.Close()
'Vraća podatke u Listi stringova
'Return result
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Return Nothing
Finally
ManageConnection(True, konekcija)
End Try
End Function
So i can use it in next function with for each loop:
Dim s As String = Globals.prodaja
Dim parts As String() = s.Split(New Char() {","c})
Dim icona As String = parts(1)
Dim barmanager1 As New BarManager
Dim TileBarItem = New TileBarItem()
TileBarItem.Content = parts(3)
TileBarItem.Name = "ffss"
TileBarItem.Width = 150
Icon = New BitmapImage(New Uri("pack://application:,,,/DevExpress.Images.v16.1;component/Images/" + icona + ""))
TileBarItem.TileGlyph = Icon
TileBarItem.Background = New SolidColorBrush(DirectCast(ColorConverter.ConvertFromString(parts(2)), Color))
MessageBox.Show(parts(2))
maloprodaja.Items.Add(TileBarItem)
Right now i have to run function for each variable i have stored in global variables class, i would like to add all the results from first function to one array in Globals and then run second function with for each loop to populate my tilebar
Using an external variable like Globals to pass data around is extremely poor practice. Your first function in the question should return the data, or alternatively it should return the MySqlDataReader. That will simplify what you're trying to do later on and effectively make this problem go away.
I also so saw this:
Dim strQuery As String = "SELECT * FROM info.opcije_mp as mp inner join instalacije as i where mp.idopcije_mp =
i.opcijeMP and i.instalacije_hwid = '" + Globals.cpuid + "';"
I want to highlight this part:
" ... and i.instalacije_hwid = '" + Globals.cpuid + "';"
It's hard to understand just how bad that is. I can't think of a better way to get a program hacked. Google for parameterized queries and learn how to use them, rather than string concatentation, to put your cpuid into the sql statement.
Related
How to evaluate match on the record table with vb I have a code like this
Dim absenperintah As SqlCommand = New SqlCommand("Select * from mytable where kodetp ='109'", cekdata)
absenperintah.CommandType = CommandType.Text
cekdata.Open()
Dim valuex = "500"
Using cekbaca As SqlDataReader =
absenperintah.ExecuteReader(CommandBehavior.CloseConnection)
If cekbaca.Read = True Then
Dim nilxbaca = cekbaca("rumus_k") --> record in the table is 1906650*(3.7/100)
Dim nilmy = valuex + nilxbaca
Dim result = New DataTable().Compute(nilmy, Nothing)
MsgBox(result)
End If
cekbaca.Close()
End Using
cekdata.Close()
I get the result 185,070,546 it should be result =70,564.55
how to get result =70,564.55 from Dim nilHy = valuex + nilxbaca
Your problem is that you are declaring these numbers as strings and they are being calculated accordingly.
What you think you are doing:
500 + 1906650*(3.7/100) = 70,564.55
If you were processing these as numbers, it would perform math and your calculation would work.
However, you have declared these values as strings. Therefore the code is actually running like this:
"500" + "1906650*(3.7/100)" 'String concatenation. Not math.
5001906650*(3.7/100) = 185,070,546
Try this code instead:
Dim absenperintah As SqlCommand = New SqlCommand("Select * from mytable where kodetp ='109'", cekdata)
absenperintah.CommandType = CommandType.Text
cekdata.Open()
Dim valuex = "500"
Using cekbaca As SqlDataReader =
absenperintah.ExecuteReader (CommandBehavior.CloseConnection)
If cekbaca.Read = True Then
Dim nilxbaca = cekbaca("rumus_k") 'record in the table is "1906650*(3.7/100)"
Dim nilmy = valuex & " + " & nilxbaca 'makes "500 + 1906650*(3.7/100)"
'Note: your next line of code, DataTable.Compute() will evaluate a
' (string) formula over several rows.
Dim result = New DataTable().Compute(nilmy, Nothing)
'Based on this code here, you are only processing one row.
'Maybe .Compute() is totally unnecessary here.
'You would be better to say: result = 500 + nilxbaca
' or maybe declare valuex as integer, to help the compiler treat it like a number instead of a string.
' or maybe your original code did apply this formula over several rows.
' ... In that case, my example would fix your Compute() formula.
MsgBox(result)
End If
cekbaca.Close()
End Using
cekdata.Close()
I am going through legacy code and not that familiar with VB or Webservices tbh. I need this to display as an array but when running the operation, it will not return the actual list items. I get the following instead:
anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/" d1p1:type="q1:string">[]</anyType
What am I missing?
Public Shared Function GetVehicleFromVINforDecode(ByVal vin As String)
Dim VINlist As New List(Of VINResult)
Dim strPattern As String
strPattern = GetVinPattern(vin)
Using conn As New SqlConnection(ConnectionStrings.CKConnectionString)
Dim sqlComm As New SqlCommand("select * from VehicleData.dbo.vin_pattern where vin_pattern = '" & strPattern & "'", conn)
conn.Open()
Using r As SqlDataReader = sqlComm.ExecuteReader
While r.Read()
Dim VINitem = New VINResult()
VINitem.Make = (r("Make"))
VINitem.Model = (r("Model"))
VINitem.Year = (r("Year"))
VINitem.Cylinders = (r("DEF_ENGINE_CYLINDERS"))
VINitem.Drive = (r("DRIVE_TYPE"))
VINitem.Liters = (r("DEF_ENGINE_SIZE"))
VINitem.Style = (r("Style"))
VINitem.TransmissionSpeed = (r("DEF_TRANS_SPEEDS"))
VINitem.TransmissionType = (r("DEF_TRANS_TYPE"))
VINitem.Trim = (r("Trim"))
VINitem.FuelType = (r("FUEL_TYPE"))
VINlist.AddRange(VINitem)
End While
End Using
End Using
Return VINlist
End Function
A List(Of T) and an array are not the same things, and from my experience here on Stack Overflow web services usually don't have very good support for the former.
Try calling ToArray() on the list when returning it in order to convert it into a regular array:
Return VINlist.ToArray()
I have rowfilter which i use to filter my datasource. I have couple controls which user can pick up checking specific checkboxes either to be used for Rowilter or not. Below code.
Dim ds = CType(dgvAbmessungen.PrimaryGrid.DataSource, DataSet)
Dim dtMain As DataTable = ds.Tables(0)
Dim filterStr As String = String.Empty
Dim byNummer As String = " [Nummer] like '" & txtFilterByNummer.Text.Trim() & "%'"
Dim byUser As String = " [User] = '" + cbUser.Text
Dim byCreateDateFrom As String = " [CreateDate] >= '" + CType(Convert.ToDateTime(calFrom.Value.Date), String)
Dim byCreateDateTo As String = "' ([CreateDate] < '" + CType(Convert.ToDateTime(calTo.Value.Date), String)
'select case checboxes and construct final rowfilter's string
dtMain.DefaultView.RowFilter = filterStr
Checboxes for diffrent controls to be checked:
for Nummer there is checkboxNummer
for User there is checkboxUser
for CreateDateFrom there is checkboxCreateDateFrom
for CreateDateTo there is checkboxCreateDateTo
Target is user checks one or more checboxes and filter should be constructed (string). Problem is i have no idea how to also concat "And" keywords between strings if user check more than one diffrent checboxes.
Currently i try to do it using select case. What could be the most efficient way to do so?
I would use LINQ:
Dim data = dtMain.AsEnumerable()
If checkboxNummer.Checked Then
data = data.Where(Function(row) row.Field(Of String)("Nummer").Contains(txtFilterByNummer.Text.Trim()))
End If
If checkboxUser.Checked Then
data = data.Where(Function(row) row.Field(Of String)("User") = cbUser.Text.Trim())
End If
If CreateDateFrom.Checked Then
data = data.Where(Function(row) row.Field(Of Date)("CreateDate") >= calFrom.Value.Date)
End If
If CreateDateTo.Checked Then
data = data.Where(Function(row) row.Field(Of Date)("CreateDate") < calTo.Value.Date)
End If
If you need the result as DataRow() use data.ToArray(). If you need a DataTable use:
If data.Any() Then dtMain = data.CopyToDataTable()
I have recently noticed on my performance assist tool I use on Visual Studio 2013 any time I make a complex string
as a sample string:
"SELECT * from calls where randid = '" & randid & "'"
it will instead recommend using string format
String.Format("SELECT * from calls where randid = '{0}'", randid)
same with using value initializers
instead of
Dim cb As New MySqlConnectionStringBuilder
cb.Database = dbfile
cb.Server = dbserver
cb.UserID = dbuser
cb.Password = dbpassw
use
Dim cb As New MySqlConnectionStringBuilder() With {.Database = dbfile, .Server = dbserver, .UserID = dbuser, .Password = dbpassw}
Do these formats actually improve the performance of an application or is it recommending these for aesthetics?
Out of curiosity (I am absolutely sure that mini-optimizations are devilish) I have tested the difference with this code using LinqPad:
Sub Main
Dim test = "999"
Dim sw = new Stopwatch()
sw.Start()
for i = 0 to 100000
Dim s = "SELECT * FROM TABLE WHERE FIELD = '" + test + "'"
Next
sw.Stop()
sw.Elapsed.Dump("Concat")
sw = new Stopwatch()
sw.Start()
for i = 0 to 100000
Dim s = string.Format("SELECT * FROM TABLE WHERE FIELD = '{0}'", test)
Next
sw.Stop()
sw.Elapsed.Dump("Format")
End Sub
with these results:
Concat
00:00:00.0101903
Format
00:00:00.0365234
The output changes noticeably if we use an integer for the test variable because now the concatenation should use ToString()
Dim test = 999
Concat
00:00:00.0198107
Format
00:00:00.0485193
So, for whatever reason your tool suggest to use the string.Format approach it is not to get better performance.
In the first one compiler will generate String.Concat method call for your string concatenation:
return "test" + value + "test";
it translated into
IL_0000: ldstr "test"
IL_0005: ldarg.0
IL_0006: box [mscorlib]System.Int32
IL_000b: ldstr "test"
IL_0010: call string [mscorlib]System.String::Concat(object, object, object)
IL_0015: ret
Both String.Concat and String.Format will cause boxing (I assumed randid is an int). You should call ToString() on it anyway to make it better.
The second one is a lay. Compiler will generate properties assignments anyway, so
Dim cb As New MySqlConnectionStringBuilder() With {.Database = dbfile, .Server = dbserver, .UserID = dbuser, .Password = dbpassw}
is transformed into
Dim cb As New MySqlConnectionStringBuilder
cb.Database = dbfile
cb.Server = dbserver
cb.UserID = dbuser
cb.Password = dbpassw
by compiler.
Dim myReader As OleDbDataReader
Dim Index As Integer
Dim status As Array
Index = 0
cmd.CommandText = "SELECT CPALLOCATIONTIME from RECORDMASTER where ID='" & TxtID.Text & "'"
cmd.CommandType = CommandType.Text
myReader = cmd.ExecuteReader()
Do While myReader.Read()
status(Index) = myReader.Item(0)
Index = Index + 1
Loop
myReader.Close()
If (Index = 2) Then
If ((status(0) = "Fp" Or status(0) = "Op") And status(1) = "OXp") Then
qText = TxtSTS.Text + "X"
Update = True
ApplicationStatus = 2
ElseIf ((status(0) = "Fp" Or status(0) = "Op") And status(1) = "FXp") Then
qText = TxtSTS.Text + "X"
Update = True
ApplicationStatus = 2
End If
Can some one please help me with status(Index) = myReader.Item(0), giving an error with conversion
You want your array to grow as elements are added. That's not what arrays are for. Use a List(Of T) instead. (See the examples on MSDN for the exact syntax.)
Make sure the data read from the reader has the correct data type. You have two ways of doing that:
Cast it (e.g. DirectCast(myReader(0), String)) or
(better) use the reader method that already returns the correct data type, e.g. myReader.GetString(0).
Redim status(0) ' < -- declare like this
Then in Loop
Redim preserve status(index)
status(Index) = myReader("Column_name")
index = index + 1
Try
status(Index) = myReader.Item(0).ToString