Adding a character before a specific character in a string - vb.net

Im developing a CRUD in vb.net using a MySQL database with a DataGridView and when inserting serial numbers some have a ' which conflits with MySQL. What I need is: the program to check if there is a ' in the string and add a \ behind if it doesn't have it
If Me.DataGridView1.Rows.Count = 0 OrElse Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim res As DialogResult = MsgBox("EDIT product?", MessageBoxButtons.YesNo)
If res <> DialogResult.Yes Then Exit Sub
For Each row As DataGridViewRow In DataGridView1.SelectedRows
frm2.Label10.Text = row.Cells(0).Value

This is the pattern,
Dim s As String = "a ' in the string and add a \ behind if it doesn't have it"
If s.Contains("'") Then
s = s.Replace("'", "'\")
End If
Replaces all occurrences of ' .

Related

Index was outside the bounds of the array. VB.NET

My problem
Index was outside the bounds of the array. when i try to run the code , it generates this error
i have two forms : SIGN IN and SIGN UP , my problem is they don't work together and generates the error attached below
Dim fs As New FileStream("C:\Users\Selmen\Desktop\vb\logs.txt", FileMode.Open, FileAccess.ReadWrite)
Dim sr As New StreamReader(fs)
Dim sw As New StreamWriter(fs)
Dim s As String
Dim t() As String
Dim trouve As Integer = 0
Dim tt() As String
Dim ch As String
ch = TextBox1.Text + "#" + TextBox2.Text + "#" + TextBox3.Text + "#" + TextBox4.Text + "#" + TextBox5.Text
tt = ch.Split("#")
Do While (trouve = 0) And (sr.Peek > -1)
s = sr.ReadLine
t = s.Split("#")
If String.Compare(t(2), tt(2)) = 0 Then
trouve = 1
End If
Loop
If (trouve = 1) Then
MsgBox("user existant")
Else
sw.WriteLine(ch)
Me.Hide()
Form4.Show()
End If
sw.Close()
sr.Close()
fs.Close()
End Sub
If String.Compare(t(2), tt(2)) = 0 Then I get:
IndexOutOfRangeException was unhandled / Index was outside the bounds of the array.
Streams need to be disposed. Instead of using streams you can easily access a text file with the .net File class.
File.ReadAllLines returns an array of lines in the file. We can loop through the lines in a For Each. The lower case c following the "#" tells the compiler that you intend a Char not a String. String.Split expects a Char. Normally, String.Compare is used to order strings in alphabetical order. You just need an =. As soon as we find a match we exit the loop with Exit For.
We don't actually need the array of the text boxes Text property unless there is no match. Putting the elements in braces intializes and fills the array of String.
File.AppendAllLines does what it says. It is expecting an array of strings. As with the text boxes, we put our line to append in braces.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim p = "path to file"
Dim lines = File.ReadAllLines(p)
Dim trouve As Integer
For Each line In lines
Dim thirdField = line.Split("#"c)(2)
If thirdField = TextBox3.Text Then
trouve = 1
Exit For
End If
Next
If trouve = 1 Then
MsgBox("user existant")
Else
Dim tt = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text}
File.AppendAllLines(p, {String.Join("#", tt)})
Me.Hid3e()
Form4.Show()
End If
End Sub

vb.net DataGridView prevent user clicking on column

I have a DataGridView (DGV) that I am adding items to from a SQLite DB MANUALLY
My setting for the Selection Mode have been CellSelect and FullRowSelect may have tried others
The Errors and Issues are varied depending on where the user clicks on the DGV
The code as it is now the Errors only occur when an empty Column under Header PID is selected
And when the Column Header PID is selected See Posted Image
I have included the ERRORS as comments in the code
One of the issues I would like to FIX is to disable all sorting on all Columns
The Data added to the DGV is a primary key which is a Integer PID
And some text which is a NVARCHAR(2048) this text is from a RichTextBox
The two biggest issue to fix is preventing the ERRORS YES those are the Questions
Private Sub PopulateDGV()
For Each header As DataGridViewHeaderCell In dgvOne.Rows
'header.SortMode = DataGridViewColumnHeaderCell.NotSortable
Next
'The code Above NOT Working
'ERRORS
'System.InvalidOperationException 'Column's SortMode cannot be set to Automatic
'While the DataGridView control's SelectionMode is set to ColumnHeaderSelect.'
'Your app has entered a break state, but there is no code to show because all
'threads were executing external code (typically system Or framework code).
'dgvOne = DataGridViewColumnSortMode.NotSortable
Dim str2 As String
Dim s1 As Integer
Dim dbName As String = "Word.db"
Dim conn As New SQLiteConnection("Data Source =" & dbName & ";Version=3;")
Dim valuesList As ArrayList = New ArrayList()
'Read from the database
Dim cmd As SQLiteCommand = New SQLiteCommand("Select * FROM ParentTable", conn)
conn.Open()
Dim rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
'Set Design of the DataGridView
dgvOne.DefaultCellStyle.Font = New Font("Tahoma", 10)
dgvOne.ColumnCount = 2
dgvOne.Columns(0).Width = 60
dgvOne.Columns(1).Width = 420
'Set Col Header Size Mode = Enabled
'Set Col Header Default Cell Styles DO in Properties
dgvOne.ColumnHeadersHeight = 34
'DGV Header Names
dgvOne.Columns(0).Name = "PID"
dgvOne.Columns(1).Name = "Entry Data"
'Read from DB Table add to DGV row
While rdr.Read()
valuesList.Add(rdr(1)).ToString()
lbOne.Items.Add(rdr(1)).ToString()
s1 = rdr(0).ToString
str2 = rdr(1)
dgvOne.Rows.Add(s1, str2)
End While
'Add Blank rows to DGV
For iA = 1 To 4
dgvOne.Rows.Add(" ")
Next
rdr.Close()
conn.Close()
End Sub
Private Sub dgvTwo_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvOne.CellMouseClick
If (e.RowIndex = -1) Then
MessageBox.Show("No Clicking Here")
Return
End If
If dgvOne.CurrentCell.Value Is Nothing Then
tbMessage.Text = "NO Data Here"
Return
ElseIf e.RowIndex >= 0 Then
tbMessage.Text = e.RowIndex
Dim str2 As String
Dim s2 As String
Dim row As DataGridViewRow = dgvOne.Rows(e.RowIndex)
s2 = row.Cells(0).Value.ToString
str2 = row.Cells(1).Value.ToString
strB = str2
rtbEnter.Text = strB
'ERRORS
'System.NullReferenceException
'HResult = 0x80004003
'Message = Object reference Not Set To an instance Of an Object.
'Source = TestSQL
'StackTrace:
'at TestSQL.frmThree.dgvTwo_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e) in C:\Users\Dwight\source\repos\TestSQL\TestSQL\frmThree.vb:line 117
End If
End Sub
To prevent sorting on columns, which appears to be your main question, do the following:
dgvOne.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
dgvOne.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
You also need to guard against null values in cells. So in the "CellMouseClick" event, add something like this:
If row.Cells(1).Value IsNot Nothing Then
str2 = row.Cells(1).Value.ToString
End If
#Brian M Stafford
Thanks for getting us headed in the correct direction Vector this is easy to figure out if someone would have shared a little information GREAT Questions's guess your were saving bytes
Because you only have three (3) Columns here are their names and positions in the DGV
Col -1 Col 0 Col 1 and YES they go Left to Right
Just So No One Gets Confused Rows go Top to Bottom
Row 0
Row 1
So all you really needed was one more test that delt with ColumnIndex
This code only permits clicking on Column 1 provided it has text in that CELL
Just manipulate the tests and you can provide the user additional selections
Public Sub dgvTwo_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvOne.CellMouseClick
If e.RowIndex = -1 Or e.ColumnIndex = -1 Then
'tbMessage.Text = "Col " & e.ColumnIndex & " ROW " & e.RowIndex
tbMessage.Text = "No Clicking Here"
Return
End If
If dgvOne.CurrentCell.Value Is Nothing Then
tbMessage.Text = "NO Data Here"
Return
End If
Dim s3 As Integer
s3 = e.ColumnIndex
If s3 = 0 Then
'tbMessage.Text = "S3 " & e.ColumnIndex
tbMessage.Text = "No Clicking Here"
Return
End If
Dim row As DataGridViewRow = dgvOne.Rows(e.RowIndex)
Dim col As DataGridViewColumn = dgvOne.Columns(e.ColumnIndex)
If e.RowIndex >= 0 And e.ColumnIndex <> -1 Then
tbMessage.Text = "Data Sent to RTB"
Dim str2 As String
str2 = row.Cells(1).Value.ToString
rtbEnter.Text = str2
End If
End Sub
After reading all the nice answers and looking for a way to accomplish my multitude of issues and a lot of trial and error here is one way to deal with preventing the user from clicking on various location on the DataGridView
Public Sub dgvTwo_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvOne.CellMouseClick
If e.RowIndex = -1 Or e.ColumnIndex = -1 Then
'tbMessage.Text = "Col " & e.ColumnIndex & " ROW " & e.RowIndex
tbMessage.Text = "No Clicking Here"
rtbEnter.Clear()
Return
End If
Dim str1 As String
Dim str2 As String
Dim s1 As Integer
Dim row As DataGridViewRow = dgvOne.Rows(e.RowIndex)
str1 = dgvOne.CurrentRow.Cells(0).Value.ToString
If str1 IsNot " " Then
If str1 Is " " Then
Return
End If
tbMessage.Text = "Text Sent to RTB"
s1 = row.Cells(0).Value
'Dim strInt As String
gv_passInt = s1.ToString
str2 = row.Cells(1).Value.ToString
rtbEnter.Text = str2
gv_passStr = str2
Return
End If
rtbEnter.Clear()
tbMessage.Text = "No Data Here"
End Sub

Repeat character in Two or More Textboxes VB Net

I want to compare the Textbox1 with TextBox2, or Textbox line 1 of the text box to the 2nd line, to show me the existing Character in another textbox, or show me how many characters are repeated. iI really like learning, so I would be helpful because I want to learn...
TextBox1.Text = 1,4,7,11,13,16,19,20,28,31,44,37,51,61,62,63,64,69,71,79,80
TextBox2.Text = 1,5,7,10,13,16,26,20,28,31,44,37,51,72,73,74,69,71,79,80
TextBox3.Text = Character Repeated: 1,7,13,16,20,28,31,44,37,51,69,71,79,80
TextBox4.Text = Number of Character Repeated = 14
TextBox5.Text = Number of Character which has not been repeated: 4,11,19,61,62,63,64 etc, you got to idea
TextBox6.Text = Number of Character isn't Repeated: 7
here are some codes: but I do not know how to apply them correctly.
Code 1: Show repetable character:
' Split string based on space
TextBox1.Text = System.IO.File.ReadAllText(Mydpi.Text)
TextBox2.Text = System.IO.File.ReadAllText(Mydpi.Text)
TextBox4.Text = System.IO.File.ReadAllText(Mydpi.Text)
For i As Integer = 0 To TextBox2.Lines.Count - 1
Dim textsrtring As String = TextBox4.Lines(i)
Dim words As String() = textsrtring.Split(New Char() {","c})
Dim found As Boolean = False
' Use For Each loop over words
Dim word As Integer
For Each word In words
TxtbValBeforeCompar.Text = TextBox1.Lines(i)
CompareNumbers()
If TextBox1.Lines(i).Contains(word) Then
found = True
Dim tempTextBox As TextBox = CType(Me.Controls("Checkertxt" & i.ToString), TextBox)
On Error Resume Next
If TextBox2.Lines(i).Contains(word) Then
If tempTextBox.Text.Contains(word) Then
Else
tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
End If
Else
End If
End If
Next
Next
Private Sub CompareNumbers()
'First Textbox that is to be used for compare
Dim textBox1Numbers As List(Of Integer) = GetNumbersFromTextLine(N1Check.Text)
'Second Textbox that is to be used for compare
Dim textBox2Numbers As List(Of Integer) = GetNumbersFromTextLine(TxtbValBeforeCompar.Text)
'Union List of Common Numbers (this uses a lambda expression, it can be done using two For Each loops instead.)
Dim commonNumbers As List(Of Integer) = textBox1Numbers.Where(Function(num) textBox2Numbers.Contains(num)).ToList()
'This is purely for testing to see if it worked you can.
Dim sb As StringBuilder = New StringBuilder()
For Each foundNum As Integer In commonNumbers
sb.Append(foundNum.ToString()).Append(" ")
TxtbValAfterCompar.Text = (sb.ToString())
Next
End Sub
Private Function GetNumbersFromTextLine(ByVal sTextLine As String) As List(Of Integer)
Dim numberList As List(Of Integer) = New List(Of Integer)()
Dim sSplitNumbers As String() = sTextLine.Split(" ")
For Each sNumber As String In sSplitNumbers
If IsNumeric(sNumber) Then
Dim iNum As Integer = CInt(sNumber)
TxtbValAfterCompar.Text = iNum
If Not numberList.Contains(iNum) Then
TxtbValAfterCompar.Text = ("")
numberList.Add(iNum)
End If
Else
End If
Next
Return numberList
End Function
Code 2: Remove Duplicate Chars (Character)
Module Module1
Function RemoveDuplicateChars(ByVal value As String) As String
' This table stores characters we have encountered.
Dim table(value.Length) As Char
Dim tableLength As Integer = 0
' This is our result.
Dim result(value.Length) As Char
Dim resultLength As Integer = 0
For i As Integer = 0 To value.Length - 1
Dim current As Char = value(i)
Dim exists As Boolean = False
' Loop over all characters in the table of encountered chars.
For y As Integer = 0 To tableLength - 1
' See if we have already encountered this character.
If current = table(y) Then
' End the loop.
exists = True
y = tableLength
End If
Next
' If we have not encountered the character, add it.
If exists = False Then
' Add character to the table of encountered characters.
table(tableLength) = current
tableLength += 1
' Add character to our result string.
result(resultLength) = current
resultLength += 1
End If
Next
' Return the unique character string.
Return New String(result, 0, resultLength)
End Function
Sub Main()
' Test the method we wrote.
Dim test As String = "having a good day"
Dim result As String = RemoveDuplicateChars(test)
Console.WriteLine(result)
test = "areopagitica"
result = RemoveDuplicateChars(test)
Console.WriteLine(result)
End Sub
End Module
You could make use of some LINQ such as Intersect and Union.
Assuming your TextBox1 and TextBox2 contains the text you have provided.
Here's a simple method to find repeated and non repeated characters.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim firstBoxList = TextBox1.Text.Split(",").ToList()
Dim secondBoxList = TextBox2.Text.Split(",").ToList()
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
For Each str As String In intersectionList
TextBox3.Text = TextBox3.Text & str & ","
Next
TextBox4.Text = intersectionList.Count()
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
For each str As String In notRepeatedCharacter
TextBox5.Text = TextBox5.Text & str & ","
Next
TextBox6.Text = notRepeatedCharacter.Count()
End Sub
The output is something like that:
This consider both of the textboxes not repeated character.
If you just want to find the not repeated characters from first list to the second, this should do it:
firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
For Each str As String In firstBoxList
TextBox7.Text = TextBox7.Text & str & ","
Next
TextBox8.Text = firstBoxList.Count
And this is the output:
Here's the full code using String.Join to make the lists look smoother in the text boxes:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'First we grab all the numbers written inside the textboxes (I am not verifying anything)
Dim firstBoxList = TextBox1.Text.Split(",").ToList()
Dim secondBoxList = TextBox2.Text.Split(",").ToList()
'Second we intersect the two lists and show them
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
TextBox3.Text = String.Join(",", intersectionList)
TextBox4.Text = intersectionList.Count()
'We're checking the distintc character from both lists
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
TextBox5.Text = String.Join(",", notRepeatedCharacter)
TextBox6.Text = notRepeatedCharacter.Count()
'we're checkng the distinct character inside first list that doesn't show in second list
firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
TextBox7.Text = String.Join(",", firstBoxList)
TextBox8.Text = firstBoxList.Count
End Sub

Deleting multiple records from sql bound Datagrid

The procedures below allowed me to delete several records at once by checking the checkbox on my datagrid. The procedure was written on ASP.net but now I am using a winform on VB.net.
I have a datagrid with column name "Delete" where the checkboxes are located. The user would check
the records it wants to delete and the would delete those records. I use the "Ticket Number" column values as the parameter for my query.
The issue I have is that since was written for ASP.Net, I cannot find how the winform VB.net equivalent for this line:
Dim chkDelete As CheckBox = DirectCast(grdRoster.Rows(i).Cells(0).FindControl("Delete_Row"), CheckBox)
FindControl is not a member of System.Windows.Forms.DataGridViewCell. Plus I am pretty sure that the whole line is wrong since the checkboxes
are located on a datagrid column set as ColumnType: DataGridViewCheckBoxColumn and are not really individual controls.
How can I get the same result on a winform? Here is my entire code.
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'Create String Collection to store
'IDs of records to be deleted
Dim ticketNumberCollection As New StringCollection()
Dim strTicketNumber As String = String.Empty
'Loop through GridView rows to find checked rows
For i As Integer = 0 To grdRoster.Rows.Count - 1
Dim chkDelete As CheckBox = DirectCast(grdRoster.Rows(i).Cells(0).FindControl("Delete_Row"), CheckBox)
If chkDelete IsNot Nothing Then
If chkDelete.Checked Then
strTicketNumber = grdRoster.Rows(i).Cells(1).ToString
ticketNumberCollection.Add(strTicketNumber)
End If
End If
Next
'Call the method to Delete records
DeleteMultipleRecords(ticketNumberCollection)
' rebind the GridView
grdRoster.DataBind()
End Sub
' Sub to delete multiple records
' #param "idCollection" calls the string collection above
' and deletes the selected record separated by ","
Private Sub DeleteMultipleRecords(ByVal ticketNumberCollection As StringCollection)
Dim IDs As String = ""
'Create string builder to store
'delete commands separated by ,
For Each id As String In ticketNumberCollection
IDs += id.ToString() & ","
Next
Try
Dim strTicketID As String = IDs.Substring(0, IDs.LastIndexOf(","))
DataSheetTableAdapter.DeleteRecord(strTicketID)
Catch ex As Exception
Dim errorMsg As String = "Error in Deletion"
errorMsg += ex.Message
Throw New Exception(errorMsg)
Finally
Me.Close()
End Try
End Sub
for deleting multiple records from a data bound gridview you should create the DataGridViewCheckBoxColumn at run time
Dim chk As New DataGridViewCheckBoxColumn()
DataGridView1.Columns.Add(chk)
chk.HeaderText = "Select"
'then bind your datagridview with dataset
Dim sql As String = "SELECT * FROM table_name"
' Dim connection As New SqlConnection(connectionString)
conn.Open()
sCommand = New SqlCommand(sql, conn)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "table_name")
sTable = sDs.Tables("table_name")
DataGridView1.DataSource = sDs.Tables("table_name")
'then traverse through each column and get the checked values
Try
DataGridView1.EndEdit()
For j = Me.DataGridView1.Rows.Count - 1 To 0 Step -1
If Not IsDBNull(DataGridView1.Rows(j).Cells(0).Value) Then
If DataGridView1.Rows(j).Cells(0).Value = True Then
check = True
If MessageBox.Show("Do you want to delete these records?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
For i = Me.DataGridView1.Rows.Count - 1 To 0 Step -1
If Not IsDBNull(DataGridView1.Rows(i).Cells(0).Value) Then
If DataGridView1.Rows(i).Cells(0).Value = True Then
'remove the checked columns and update datatable
DataGridView1.Rows.RemoveAt(i)
sAdapter.Update(sTable)
End If
End If
Next
Else
Return
End If
Else
End If
End If
Next
If check = False Then
MsgBox("Nothing Selected")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try

VB.Net Satisfy if condition from array BEFORE else statement executes

This is killing me because I know why it's doing it but I don't know how to stop it. I am reading from a text file where I have 2 users on 2 lines: bill|777 & john|333.
My conditional statement satisfies both conditions because when it loops thru, it declines one user and accepts the other causing it to do the if and the else. Please tell me how to do this one at a time. Loop thru the text, get the proper user and then go thru the conditions.
Dim MyReader As New StreamReader("login.txt")
While Not MyReader.EndOfStream
Dim user As String = UsernameTextBox.Text + "|" + PasswordTextBox.Text
Dim names() As String = MyReader.ReadLine().Split()
For Each myName In names
If user = myName Then
Me.Hide()
OrderForm.Show()
Else
MsgBox("Wrong username and password")
End If
Next
End While
MyReader.Close()
Something like this should work:
Using MyReader As New StreamReader("login.txt")
Dim GoodUser As Boolean = False
Dim user As String = UsernameTextBox.Text + "|" + PasswordTextBox.Text
While Not MyReader.EndOfStream
Dim user As String = UsernameTextBox.Text + "|" + PasswordTextBox.Text
Dim names() As String = MyReader.ReadLine().Split()
If Not names Is Nothing Then
For Each myName In names
If user = myName Then
GoodUser = True
Me.Hide()
OrderForm.Show()
Exit While
End If
Next
End If
End While
If Not GoodUser Then
MsgBox("Wrong username and password")
End If
End Using
The using block automatically disposes of the streamreader. A boolean to signify a good login can set the condition when the While loop exits. The Exit While will break out of the loop when the proper user is found. It's usually a good idea to set a conditional to check for empty lines
One thing to watch for. If a user name includes a space your code won't work. You'll have to either restrict the user names or use a different delimiter like ~.
Try this code:
Using r As StreamReader = New StreamReader("login.txt")
Dim line As String = r.ReadLine
Dim user As String = UsernameTextBox.Text + "|" + PasswordTextBox.Text
Dim found As Boolean = False
Do While (Not line Is Nothing)
If (line = user) Then
found = True
break
End If
line = r.ReadLine
Loop
If (Not found) Then
MessageBox.Show("Wrong username and password")
End If
End Using