I have three strings. 1. str1="450" 2. str2="SKDR" 3. str3="008001". I want to join these three strings. Each time str3 value should be incremented by 1. If we take it as integer then intial zeroes will be truncated.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim d1 As String = ""
Dim s As String = ""
Dim dtqry1 As Integer = 0
Dim br As String = dgvCanaraBankCAU.CurrentRow.Cells(2).Value.ToString
Dim pro As String = dgvCanaraBankCAU.CurrentRow.Cells(3).Value.ToString
Dim dtqry As DataTable = mainModule.DatabaseTableQuery("SELECT DPNO,StartNumber from Master_CINumber where Branch='" + br + "' and ProjectOffice='" + pro + "'")
If dtqry.Rows.Count > 0 Then
For Each dr As DataRow In dtqry.Rows
d1 = dr(0).ToString
s = dr(1).ToString
Next
Dim chk As DataTable = mainModule.DatabaseTableQuery("Select * from CBEntry where BankBranch='" + br + "' and ProjectOffice='" + pro + "'")
If chk.Rows.Count = 0 Then
dtqry1 = Val(s)
Else
dtqry1 = mainModule.DatabaseScalarQuery("SELECT max(startNumber) from CBEntry where BankBranch='" + br + "' and ProjectOffice='" + pro + "'")
dtqry1 = dtqry1 + 1
End If
End If
End Sub
Use a custom format string passed to In32.ToString to include leading zeros.
Eg.
Dim x = 42
Console.WriteLine(x.ToString("000000"))
will output
000042
Related
Good morning,
I have an issue with my code :
1 - I get values from a row in a worksheet
2 - I split this value in 2 when there is a comma
For example "battery, Lithium" become "battery" and "lithium"
3 - then I store every value in 2 arrays
4 - From this array, I create a massive string with every value separated with a comma
5 - I generate my validate list from this string
ReDim tipe_tri(nb_comp)
ReDim tech_tri(nb_comp)
For comp = 0 To nb_comp
comp_no_tri = feuille.Cells(2, startC + comp)
If InStr(1, comp_no_tri, ",") = 0 Then
tipe = receuil + " " + CStr(comp_no_tri) + ","
tech = "Standard,"
Else
tipe = receuil + " " + CStr(Split(comp_no_tri, ",")(0)) + ","
tech = CStr(Split(comp_no_tri, ",")(1)) + ","
End If
tipe_tri(comp) = tipe
tech_tri(comp) = tech
Next comp
Call concatenation_tab(tipe_tri, tech_tri, feuille, endC, receuil)
tipe = creation_str(tipe_tri)
tech = creation_str(tech_tri)
Worksheets("Components").Range("B2:B" & Range("B2").End(xlDown).Row).ClearContents
Worksheets("Components").Activate 'Select the sheet containing the drop down
For index = 2 To 3000 'Define the range containing drop down on the Component sheet
Worksheets("Components").Range("B" & index).Activate 'Select the cells containing the drop down list B2:B3000
With ActiveCell.Validation
.Delete
.Add xlValidateList, Formula1:=tipe 'Fill the cell with a drop down list
End With
Next index
some function used here :
Public Sub Delete_Element_array(ByVal index As Integer, ByRef prLst As Variant)
Dim j As Integer
For j = index + 1 To UBound(prLst)
prLst(j - 1) = prLst(j) ' Move all element back one position
Next j
ReDim Preserve prLst(UBound(prLst) - 1) ' Shrink the array by one, removing the last one
End Sub
Public Sub concatenation_tab(ByRef tab_ref As Variant, ByRef tab_var As Variant, page As Worksheet, col As Integer, receuil As String)
Dim i As Integer
Dim last_comp As String
Dim last_tipe As String
last_comp = page.Cells(2, col)
last_tipe = receuil + " " + CStr(Split(last_comp, ",")(0)) + ","
Do Until tab_ref(i) = last_tipe
If tab_ref(i) = tab_ref(i + 1) Then
tab_var(i) = tab_var(i) + tab_var(i + 1)
Call Delete_Element_array(i + 1, tab_ref)
Call Delete_Element_array(i + 1, tab_var)
i = i - 1
End If
i = i + 1
Loop
End Sub
Private Function creation_str(ByRef tabb As Variant) As String
Dim char As String
Dim i As Integer
For i = 0 To UBound(tabb)
char = char + tabb(i)
Next i
creation_str = char
End Function
WHY is my dropdown list only one row and not a list ??
thanks
list error
EDIT 1
This is working since i restarted Excel
i don't know why
working
I have an unsecured query which allows for an injection, and I'm not sure how to go about parameterizing it to prevent said injection
Dim sInsertSQL As String
sInsertSQL = "Insert into tbl_userprop (Prop_Def) values "
Dim tempString As String() = PropertyDefinitions.Split("|")
For i As Integer = 1 To tempString.Length
If tempString(i - 1).Length > 0 Then
sInsertSQL = sInsertSQL + " ('" + tempString(i - 1) + "'),"
bInsert = True
End If
Next
There are up to 10 values stored in tempString and they are concatenated onto sInsertSQL as such: ('val1'), ('val2'), etc
Figured it out, update for the curious:
Dim lstParams As New Collections.Generic.List(Of SqlParameter)
Dim tempString As String() = PropertyDefinitions.Split("|")
For i As Integer = 1 To tempString.Length
If tempString(i - 1).Length > 0 Then
Dim sParamName As String = String.Format("#param{0}", i)
Dim sparam As New SqlParameter(sParamName, tempString(i - 1))
lstParams.Add(sparam)
sInsertSQL = sInsertSQL + " (" + sParamName + "),"
bInsert = True
End If
Next
Once you have split the string you know how many parameters there will be, so you can create the # items for the SQL. After that, you can add the parameters by going through the lists of parameter names and values:
Dim PropertyDefinitions = "abc|def|ghi|jkl|mno"
Dim values = PropertyDefinitions.Split({"|"c})
Dim paramNames = Enumerable.Range(0, values.Count()).Select(Function(n) $"#p{n}")
Dim paramList = String.Join(", ", paramNames.Select(Function(s) $"({s})"))
Dim sql = "INSERT INTO [tbl_userprop] (Prop_Def) VALUES " & paramList
' The following line with the sample data would output '
' INSERT INTO [tbl_userprop] (Prop_Def) VALUES (#p0), (#p1), (#p2), (#p3), (#p4)
'Console.WriteLine(sql)
Dim connStr = "YourConnectionStringHere"
Using conn As New SqlConnection(connStr)
Using cmd As New SqlCommand(sql, conn)
For i = 0 To values.Count() - 1
'TODO: Set the .SqlDbType and .Size to conform to the database definition of [tbl_userprop]. '
cmd.Parameters.Add(New SqlParameter With {.ParameterName = paramNames(i),
.Value = values(i),
.SqlDbType = SqlDbType.NVarChar,
.Size = 99})
Next
'conn.Open()
'cmd.ExecuteNonQuery()
End Using
End Using
This may have been asked and answered several times, but I have been unable to find the answer.
In CbProdukt1_SelectedIndexChanged(), I need the associated value from an array from another sub:
Private Sub CbProdukt1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbProdukt1.SelectedIndexChanged
With Me.LblEnhed1
.Text = Enhed(CbProdukt1.SelectedIndex)
.Location = New Point(230, 150)
End With
Me.Controls.Add(LblEnhed1)
End Sub
This would be easy enough if Enhed() had been public. But it is not as it is dimensioned and retrieved from Sqlite in another sub:
Dim count As Integer
sql = "SELECT COUNT(varenr) AS varenrcount FROM '" & Varedatabase & "'"
cmd = New SQLiteCommand(sql, conn)
count = cmd.ExecuteScalar
sql = "SELECT * FROM '" & Varedatabase & "'"
cmd = New SQLiteCommand(sql, conn)
reader = cmd.ExecuteReader
Dim a = 0
Dim Varenr(count + 5) As String
Dim Varenavn(count + 5) As String
Dim Enhed(count + 5) As String
While (reader.Read())
Varenr(a) = reader("varenr")
Varenavn(a) = reader("Varenavn")
Enhed(a) = reader("Enhed")
CbProdukt1.Items.Add(varenavn(a))
a += 1
End While
Public declarations are only allowed prior to subs at the top of the class.
So how do I get the Enhed(a) from sub to sub?
I am doing my homework but stuck on a part. Problem is, How can i populate seat number in array of controls(labels) using database. I already created labels and a class to retrieve all rows from database but how can i apply it in main form and populate labels.
--------------------------Class---------------------------------------
Public Shared Function getOneRow(PK As Integer) As datMovieTimes
Dim returnRow As New datMovieTimes(0)
Dim connDB As New SqlConnection
connDB.ConnectionString = Conn.getConnectionString
Dim command As New SqlCommand
command.Connection = connDB
command.CommandType = CommandType.Text
command.CommandText = SQLStatements.SELECT_1_BY_ID
command.Parameters.AddWithValue("#Key", PK)
Try
connDB.Open()
Dim dR As IDataReader = command.ExecuteReader
If dR.Read() Then
returnRow.showingID = PK
If Not IsDBNull(dR(Fields.movieID)) Then returnRow.movieID = dR(Fields.movieID)
If Not IsDBNull(dR(Fields.dateTime)) Then returnRow.dateTime = dR(Fields.dateTime)
If Not IsDBNull(dR(Fields.isActive)) Then returnRow.isActive = dR(Fields.isActive)
End If
Catch ex As Exception
Console.WriteLine(Err.Description)
End Try
Return returnRow
End Function
Public Shared Function getAllRows() As Generic.List(Of datMovieTimes)
Dim returnRows As New Generic.List(Of datMovieTimes)
Dim connDB As New SqlConnection
connDB.ConnectionString = Conn.getConnectionString
Dim command As New SqlCommand
command.Connection = connDB
command.CommandType = CommandType.Text
command.CommandText = SQLStatements.SELECT_ALL
Try
connDB.Open()
Dim dR As IDataReader = command.ExecuteReader
Do While dR.Read()
Dim Row As New datMovieTimes(0)
If Not IsDBNull(dR(Fields.showingID)) Then Row.showingID = dR(Fields.showingID)
If Not IsDBNull(dR(Fields.movieID)) Then Row.movieID = dR(Fields.movieID)
If Not IsDBNull(dR(Fields.dateTime)) Then Row.dateTime = dR(Fields.dateTime)
If Not IsDBNull(dR(Fields.isActive)) Then Row.isActive = dR(Fields.isActive)
returnRows.Add(Row)
Loop
Catch ex As Exception
Console.WriteLine(Err.Description)
End Try
Return returnRows
End Function
-----------------------------main form-----------------------------------------
Public Sub createSeat()
Dim S1 As Label
For X As Integer = 1 To _MAX_X
For Y As Integer = 1 To _MAX_Y
S1 = New Label
S1.Height = 25
S1.Width = 25
S1.BackColor = Color.LightGreen
S1.Top = 100 + (X - 1) * (S1.Height + 5)
S1.Left = 200 + (Y - 1) * (S1.Width + 5)
S1.TextAlign = ContentAlignment.MiddleCenter
S1.Text = Y.ToString
AddHandler S1.Click, AddressOf GenericLabel_Click
Me.Controls.Add(S1)
_SeatArray(X, Y) = S1
Next
Next
For X As Integer = 0 To 9
_AlphaLabel(X) = New Label
_AlphaLabel(X).Height = 25
_AlphaLabel(X).Width = 25
_AlphaLabel(X).BackColor = Color.Transparent
_AlphaLabel(X).Top = 130 + (X - 1) * (_AlphaLabel(X).Height + 6)
_AlphaLabel(X).Left = 170
_AlphaLabel(X).Text = _AlphaName(X)
Me.Controls.Add(_AlphaLabel(X))
Next
End Sub
Private Sub GenericLabel_Click(sender As Object, e As EventArgs)
Dim L As New Label
L = DirectCast(sender, Label)
If L.BackColor = Color.LightGreen Then
L.BackColor = Color.Orange
clickLess -= 1
ElseIf L.BackColor = Color.Orange Then
L.BackColor = Color.LightGreen
clickLess += 1
End If
clickCount += 1
Me.lblRemainingCount.Text = clickLess.ToString
Me.nudTicketsCount.Value = clickCount
If clickLess <= 0 Then
MsgBox("No more seats left.", MsgBoxStyle.OkOnly, "House Full")
End If
End Sub
Database pic
When creating labels, insert one more line:
S1.Name = "MyLabel" & X & Y
When accessing the label:
Dim MyCurrentLabel as Label
MyCurrentLabel = CType("MyLabel" & X & Y, Label)
Then you can do things with the current label:
MyCurrentLabel.Text = "Hello World"
current image
Now it is something like this, so it want to change the colour to red if it is paid according to database.
Thanks
I found the answer, sorry i forgot to mention it because i was busy in completing the project
----------------------Seat creation----------------------------------------
Public Sub createSeat()
Dim S1 As Label
Dim numValue As Integer = 1
For X As Integer = 1 To _MAX_X
For Y As Integer = 1 To _MAX_Y
S1 = New Label
S1.Height = 25
S1.Width = 25
S1.BackColor = Color.LightGreen
S1.Top = 180 + (X - 1) * (S1.Height + 5)
S1.Left = 200 + (Y - 1) * (S1.Width + 5)
S1.TextAlign = ContentAlignment.MiddleCenter
S1.Text = Y.ToString
' S1.Text = numValue
S1.Name = "Label" & numValue
AddHandler S1.Click, AddressOf GenericLabel_Click
Me.Controls.Add(S1)
_SeatArray(X, Y) = S1
numValue += 1
Next
Next
For X As Integer = 0 To 9
_AlphaLabel(X) = New Label
_AlphaLabel(X).Height = 25
_AlphaLabel(X).Width = 25
_AlphaLabel(X).BackColor = Color.Transparent
_AlphaLabel(X).Top = 210 + (X - 1) * (_AlphaLabel(X).Height + 6)
_AlphaLabel(X).Left = 170
_AlphaLabel(X).Text = _AlphaName(X)
Me.Controls.Add(_AlphaLabel(X))
Next
End Sub
-------------------------------populate seat number----------------------------------
Public Sub populateSeatNumber()
Dim connectionString As String = DBL.Conn.getConnectionString
Dim connection As New SqlConnection(connectionString)
connection.Open()
Dim selectStatement As String = "SELECT * FROM datTicketsSold"
Dim selectCommand As New SqlCommand(selectStatement, connection)
Dim daSoldTickets As New SqlDataAdapter(selectCommand)
Dim dsSoldTickets As DataSet = New DataSet
daSoldTickets.Fill(dsSoldTickets, "datTicketsSold")
connection.Close()
Dim dtTickets As DataTable = dsSoldTickets.Tables("datTicketsSold")
Dim row As DataRow
For Each row In dtTickets.Rows
If row(3) = True Then
CType(Controls("Label" & row(2)), Label).BackColor = Color.Red
redCounter += 1
Else
CType(Controls("Label" & row(2)), Label).BackColor = Color.Yellow
yellowCounter += 1
End If
Next
Me.lblReservedCount.Text = yellowCounter.ToString
Me.lblSoldCount.Text = redCounter.ToString
End Sub
Thanks everyone
I've this script on my program to read R component of an image and save it on an array:
Dim citra_asli As Bitmap = New Bitmap(PictureBoxAsli.Image)
Dim i As Integer = 0
Dim j As Integer = 0
Dim redValue(i, j) As Integer
ListBox3.Items.Add("Piksel--R--G--B")
For i = 0 To ((citra_asli.Height) - 1)
For j = 0 To ((citra_asli.Width) - 1)
Dim R As Integer = citra_asli.GetPixel(i, j).R
redValue(i, j) = R
ListBox3.Items.Add((i.ToString + ("," + (j.ToString + (" " + (redValue(i, j).ToString))))))
Next
Next
unfortunately I always get this error message "Index was outside the bounds of the array.". As long as I know, the redValue() array and citra_asli bitmap have identically dimension but why the error message appear? Somebody help me please or maybe there's another method to save it on an array. Thank you and please forgive me for my poor English.
This ought to be correct and i have tested it.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim citra_asli As Bitmap = New Bitmap(PictureBoxAsli.Image)
Dim x As Integer = 0
Dim y As Integer = 0
Dim R As Integer = 0
ListBox1.Items.Add("Piksel--R--G--B")
y = citra_asli.Height
x = citra_asli.Width
Dim redValue(x, y) As Integer
For y = 0 To (citra_asli.Height) - 1
For x = 0 To (citra_asli.Width) - 1
R = citra_asli.GetPixel(x, y).R
redValue(x, y) = R
ListBox1.Items.Add("[" & x.ToString & "," & y.ToString & "] " & "(" & redValue(x, y).ToString & ",grn,blu)")
Next
Next
End Sub