How to verify the string value from a richtextbox - vb.net

Original Code:
The code below always runs Case Else because the text values have an exclamation in them. It I were to remove them, then they would work again.
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text = "!exit"
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text = "!clear"
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text = "!disable"
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub
Edit
Trying the suggested method (seen below) also only runs Case Else.
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase)
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text.Equals("!clear", StringComparison.CurrentCultureIgnoreCase)
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text.Equals("!disable", StringComparison.CurrentCultureIgnoreCase)
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub

Try this instead of the Select Case:
If DeveloperCommandLine.Text.ToLower().Contains("exit") Then
ToolStripButtonClose.PerformClick()
ElseIf DeveloperCommandLine.Text.ToLower().Contains("clear") Then
MessageDisplayBounds.Text = String.Empty
ElseIf DeveloperCommandLine.Text.ToLower().Contains("disable") Then
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End If

So I figured it out by looping through all Char's in the string. Apparently Environment.NewLine, vbcr, or something similar gets added to the end of the string. To resolve this I used the following code:
Private Sub CommandLineFunctions()
Dim GetChars As New List(Of String)
For Each i As Char In DeveloperCommandLine.Text
GetChars.Add(i)
Next
GetChars.Remove(GetChars.Last)
Dim CleanInput As String = String.Join("", GetChars)
Select Case True
Case CleanInput = "!exit"
ToolStripButtonClose.PerformClick()
Case CleanInput = "!clear"
MessageDisplayBounds.Text = String.Empty
Case CleanInput = "!disable"
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub

Related

Pulling Select Case data from one sub to another

Hello all My question is: Is it possible to pull case select data from another sub? I have this in a button sub:
'Public Sub btnSearch_Click(sender As Object, e As EventArgs) Handles
btnSearch.Click
Dim strInvalid As String = txtUsername.Text
Dim frmIdenitityCheckerResults As New frmIdenitityCheckerResults()
Select Case True
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename),
0))
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add
(My.Forms.frmIdenitityCheckerResults)
my.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "John"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "1211"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "1"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 2))
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add
(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "Terri"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "4218"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "2"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"'
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 1))
Link_lbl1.Text = "John"
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3))
Link_lbl1.Text = "Terri"
Link_lbl2.Text = "Jordan"
Link_lbl3.Text = "Lisa"
Link_lbl4.Text = "David"
I'm wanting to pull data from this Select Case to input in the code below
Private Sub link_lbl1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles Link_lbl1.LinkClicked
Select Case
Case Link_lbl1.Text = "John"
btnSearch 1
Case Link_lbl1.Text = "Terri"
End Select
End Sub'
But I'm unsure if this is a possibility or not if anyone can point me in the right direction It'll be much appreciated.
I attempted:
Select Case link_lbl1.text
Case "John"
But that threw an error and wasn't pulling data from my previous case select.
I can make it work using the below but didn't know if there was an easier way to go about it.
Private Sub link_lbl1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles Link_lbl1.LinkClicked
Select Case True
Case Link_lbl1.Text = "John"
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "John"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "1211"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "1"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
Case Link_lbl1.Text = "Terri"
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "Terri"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "4218"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "2"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
End Select
End Sub'
As suggested, your Select Case is wrong to begin with. Rather than this:
Select Case True
Case Link_lbl1.Text = "John"
'...
Case Link_lbl1.Text = "Terri"
'...
End Select
You should be doing this:
Select Case Link_lbl1.Text
Case "John"
'...
Case "Terri"
'...
End Select
As for the question, I assume that what you mean is that you want to avoid repeating yourself in each Case block. If so then the first thing to do is to identify what is common and what is specific. You can copy the common part into a method and then add a parameter for each specific part, e.g.
Private Sub DoSomething(label2Text As String, ssn As String, emp As String)
With My.Forms.frmIdenitityCheckerResults
.TopLevel = False
.Show()
.Label2.Text = label2Text
.lblSSN.Text = ssn
.lblCompany.Text = strPhoenix
.lblEMP.Text = emp
.lblStartDate.Text = "10/10/2017"
End With
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
Me.Close()
End Sub
You can then just call that method in each Case block and pass in the appropriate values for each argument.
Note that I have also used a With block to neaten up that code and reordered things more appropriately too.

Issue with combobox autocomplete in vb.net

I have a combobox populated by a datatable, the code searches for a text string located at any position of the field while the user is writing, so far no problem.
So the problem is: When I write the third character the combobox autocompletes with the first result, and there is no way to type anything else.
I have tried already using all AutocompleteMode & AutocompleteSourse properties settings and combinations.
That’s why I’m asking for help.
The code is below:
Private Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.DroppedDown = True
Cursor.Current = Cursors.Default
End If
End Sub
Thanks
Finally I got something that works well, it is not the final version, surely can be further improved, here is the code:
Public Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If ComboListadoRemitente.Text = "" Then
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.DroppedDown = False
End If
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
If ComboListadoRemitente.Items.Count <> 0 Then
ComboListadoRemitente.DroppedDown = True
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
Cursor.Current = Cursors.Default
Else
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
ComboListadoRemitente.DroppedDown = False
End If
End If
End Sub

sending bulk sms to group using vb.net

I connected recently to SMS provider API using vb.net
I have created a group table and inserted all numbers in this group and then reach each row and send trigger the API to process sending.
The sms is not reached to all group members, its only delivered successfully to the first mobile number in the group.
How to solve this problem ? I think I have to set a delay between each sending and i did with no use. my code is below :
Function GetGroupsMobileNumbers() As ArrayList
Dim MobileNumbersArrayList As New ArrayList
For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
MobileNumbersArrayList.Add(Contact.Mobile)
Next
Return MobileNumbersArrayList
End Function
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
If ddlGroup.SelectedValue = 0 Then
lbResult.Text = "No groups selected"
Exit Sub
End If
Dim MobileNumbersArrayList As ArrayList
MobileNumbersArrayList = GetGroupsMobileNumbers()
If MobileNumbersArrayList.Count = 0 Then
lbResult.Text = "Group doesnt contain numbers"
Exit Sub
End If
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then
Dim txtMsg As String
Dim smstype As Integer
If hidUnicode.Value <> "1" Then
txtMsg = txtMessage.Text
smstype = 1
Else
txtMsg = ConvertTextToUnicode(txtMessage.Text)
smstype = 2
End If
Dim x As Integer
'For Each Contact As FilsPayComponent.ContactAddress In FilsPayComponent.ContactAddress.GetAllContactAddressByGroupId(ddlGroup.SelectedValue)
For Each Contact In MobileNumbersArrayList.ToArray
Dim toMobile As String = Contact.Mobile
If toMobile.Length > 10 Then
Dim ExecArrayList As ArrayList
ExecArrayList = SendSMS(toMobile, txtMsg, smstype)
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
'-- give the excution more time
If ExecArrayList.Count < 1 Then
Threading.Thread.Sleep(1000)
End If
x = x + 1
' lbresult.Text = "Sent Successfully"
End If
Next
FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)
Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
NewsmsarchiveItem.FromMobile = txtSenderID.Text
NewsmsarchiveItem.ToMobile = "0"
NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
NewsmsarchiveItem.DateSent = DateTime.Now
NewsmsarchiveItem.Msg = txtMessage.Text
NewsmsarchiveItem.GroupCount = x
NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
NewsmsarchiveItem.Optional2 = "1"
NewsmsarchiveItem.MessageNo = messagecount.Value
Try
NewsmsarchiveItem.Addsmsarchive()
lbResult.Text = "Message sent successfully"
btnSend.Visible = False
Catch ex As Exception
lbResult.Text = ex.Message
End Try
Else
lbResult.Text = "Not enough credit, please refill "
End If
End Sub
Sub SendSMS(ByVal toMobile As String, ByVal txtMsg As String, ByVal smstype As Integer)
Dim hwReq As HttpWebRequest
Dim hwRes As HttpWebResponse
Dim smsUser As String = "xxxxxx"
Dim smsPassword As String = "xxxxxx"
Dim smsSender As String = "xxxxxx"
Dim strPostData As String = String.Format("username={0}&password={1}&destination={2}&message={3}&type={4}&dlr=1&source={5}", Server.UrlEncode(smsUser), Server.UrlEncode(smsPassword), Server.UrlEncode(toMobile), Server.UrlEncode(txtMsg), Server.UrlEncode(smstype), Server.UrlEncode(smsSender))
Dim strResult As String = ""
Try
hwReq = DirectCast(WebRequest.Create("http://xxxxx:8080/bulksms/bulksms?"), HttpWebRequest)
hwReq.Method = "POST"
hwReq.ContentType = "application/x-www-form-urlencoded"
hwReq.ContentLength = strPostData.Length
Dim arrByteData As Byte() = ASCIIEncoding.ASCII.GetBytes(strPostData)
hwReq.GetRequestStream().Write(arrByteData, 0, arrByteData.Length)
hwRes = DirectCast(hwReq.GetResponse(), HttpWebResponse)
If hwRes.StatusCode = HttpStatusCode.OK Then
Dim srdrResponse As New StreamReader(hwRes.GetResponseStream(), Encoding.UTF8)
Dim strResponse As String = srdrResponse.ReadToEnd().Trim()
Select Case strResponse
Case "01"
strResult = "success"
Exit Select
Case Else
strResult = "Error: " + strResponse
Exit Select
End Select
End If
Catch wex As WebException
strResult = "Error, " + wex.Message
Catch ex As Exception
strResult = "Error, " + ex.Message
Finally
hwReq = Nothing
hwRes = Nothing
End Try
End Sub
If function GetGroupsMobileNumbers() does not return an array list of numbers (as Strings)
then comment out. MobileNumbersArrayList = GetGroupsMobileNumbers()
then use the commented out code below (with three of your own tel. numbers) to set it for testing.
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If ddlGroup.SelectedValue = 0 Then
lbResult.Text = "No groups selected"
Exit Sub
End If
Dim MobileNumbersArrayList As New ArrayList
MobileNumbersArrayList = GetGroupsMobileNumbers()
'MobileNumbersArrayList.Add("07702123456")
'MobileNumbersArrayList.Add("07702123457")
'MobileNumbersArrayList.Add("07702123458")
If MobileNumbersArrayList.Count = 0 Then
lbResult.Text = "Group doesnt contain numbers"
Exit Sub
End If
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
If MobileNumbersArrayList.Count * messagecount.Value <= FilsPayComponent.SmSUser.GetSmSUserByUserId(Context.User.Identity.Name).Balance Then
Dim txtMsg As String
Dim smstype As Integer
If hidUnicode.Value <> "1" Then
txtMsg = txtMessage.Text
smstype = 1
Else
txtMsg = ConvertTextToUnicode(txtMessage.Text)
smstype = 2
End If
Dim x As Integer
For Each Contact In MobileNumbersArrayList
If Contact.Length > 10 Then
SendSMS(Contact, txtMsg, smstype)
x = x + 1
End If
Next
FilsPayComponent.SmSUser.RemoveSmsCredit(Context.User.Identity.Name, messagecount.Value * x)
Dim NewsmsarchiveItem As New FilsPayComponent.smsarchive
NewsmsarchiveItem.FromMobile = txtSenderID.Text
NewsmsarchiveItem.ToMobile = "0"
NewsmsarchiveItem.GroupId = ddlGroup.SelectedValue
NewsmsarchiveItem.DateSent = DateTime.Now
NewsmsarchiveItem.Msg = txtMessage.Text
NewsmsarchiveItem.GroupCount = x
NewsmsarchiveItem.Optional1 = Context.User.Identity.Name
NewsmsarchiveItem.Optional2 = "1"
NewsmsarchiveItem.MessageNo = messagecount.Value
Try
NewsmsarchiveItem.Addsmsarchive()
lbResult.Text = "Message sent successfully"
btnSend.Visible = False
Catch ex As Exception
lbResult.Text = ex.Message
End Try
Else
lbResult.Text = "Not enough credit, please refill "
End If
End Sub
This btnSend sub should work if the rest of your code is okay. Note your line.
Dim TotalNo As Integer = FilsPayComponent.ContactAddress.AddressContactsCount(ddlGroup.SelectedValue)
Doesn't appear to do anything.
If you need to set a delay you would be better off turning SendSMS into a function that returns a sent confirmation to your btnSend loop. Most texting APIs can handle lists of numbers rather than waiting for a response for each text message. Afterall they only get added to a queue at their end.

Using EF model database not Updating with updateModel

MVC 3, VB.NET, RAZOR app, using EF. I am having an issue in my post function with the database not updating at all... Using a break at db.savechanges() I look at the variable and all of the correct information is contained in the UpdateModel( ) part. But no dice the code executes and returns no errors so all looks fine but when I look at the database table it has not been changed at all, all of the old values are still present.. Function is as follows:
<AcceptVerbs(HttpVerbs.Post)>
Function EditCourse(ByVal _eCourse As cours) As ActionResult
Dim id As Integer = _eCourse.course_id
Dim _filename As String = String.Empty
Dim _guid As String = String.Empty
Dim _count As Integer = 0
Dim _courseFiles As cours = db.courses.Where(Function(f) f.course_ref = _eCourse.course_ref).First
Dim _file1 As String = _courseFiles.handoutFile1
Dim _file2 As String = _courseFiles.handoutFile2
Dim _file3 As String = _courseFiles.handoutFile3
Dim _file4 As String = _courseFiles.handoutFile4
Dim _file5 As String = _courseFiles.handoutFile5
Dim _file6 As String = _courseFiles.handoutFile6
Dim _file7 As String = _courseFiles.handoutFile7
Dim _file8 As String = _courseFiles.handoutFile8
For Each File As String In Request.Files
_count += 1
Dim hpf As HttpPostedFileBase = TryCast(Request.Files(File), HttpPostedFileBase)
If hpf.ContentLength = 0 Then
Continue For
End If
Dim savedfileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + hpf.FileName
hpf.SaveAs(savedfileName)
_filename = hpf.FileName
Select Case _count
Case Is = 1
If Not String.IsNullOrWhiteSpace(_file1) Then
If Not String.Compare(_eCourse.handoutFile1, _file1) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile1) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file1
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile1 = _filename
Case Is = 2
If Not String.IsNullOrWhiteSpace(_file2) Then
If Not String.Compare(_eCourse.handoutFile2, _file2) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile2) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file2
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile2 = _filename
Case Is = 3
If Not String.IsNullOrWhiteSpace(_file3) Then
If Not String.Compare(_eCourse.handoutFile3, _file3) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile3) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file3
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile3 = _filename
Case Is = 4
If Not String.IsNullOrWhiteSpace(_file4) Then
If Not String.Compare(_eCourse.handoutFile4, _file4) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile4) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file4
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile4 = _filename
Case Is = 5
If Not String.IsNullOrWhiteSpace(_file5) Then
If Not String.Compare(_eCourse.handoutFile5, _file5) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile5) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file5
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile5 = _filename
Case Is = 6
If Not String.IsNullOrWhiteSpace(_file6) Then
If Not String.Compare(_eCourse.handoutFile6, _file6) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile6) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file6
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile6 = _filename
Case Is = 7
If Not String.IsNullOrWhiteSpace(_file7) Then
If Not String.Compare(_eCourse.handoutFile7, _file7) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile7) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file7
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile7 = _filename
Case Is = 8
If Not String.IsNullOrWhiteSpace(_file8) Then
If Not String.Compare(_eCourse.handoutFile8, _file8) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile8) Then
Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file8
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
End If
_eCourse.handoutFile8 = _filename
End Select
Next
UpdateModel(_eCourse)
db.SaveChanges()
Return RedirectToAction("CourseIndex")
End Function
Any Ideas on why this is going wrong?????
You aren't attaching your _eCourse to your context so it won't update it.
UpdateModel I don't believe is required here at all as that simply takes your posted form values an assigns to your model which you already have since eCourse is a parameter. Do something like (on phone here sorry)
DB.Entry(_eCourse).State = EntityState.Modified
And then save changes

How to check the length value of cell in datagrid view?

I'm able to validate the cells if they are empty but I'm not able to check the length of the cell. I want the user to enter 5 digits and if it is less than 5, show up a message box.
I tried cellvalue.length method but its not working.
Private Sub dgvResults_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvResults.CellValidating
'variables
Dim columnName As String = dgvResults.Columns(e.ColumnIndex).Name
Dim cellVal As String = e.FormattedValue.ToString()
'Datagrid view validation
If ((e.ColumnIndex = 0) And (e.FormattedValue = "") And (Not (cellVal.Length = 5))) Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
ElseIf (e.ColumnIndex = 1 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf (e.ColumnIndex = 2 And e.FormattedValue = "") Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 3) And (e.FormattedValue = "") And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
ElseIf ((e.ColumnIndex = 4) And (Not IsNumeric(e.FormattedValue))) Then
e.Cancel = True
MessageBox.Show(columnName & " cannot be blank!")
End If
End Sub
Following causes your code to not work:
(e.FormattedValue = "") And (Not (cellVal.Length = 5))
You are checking if the value is empty and also it's length is <> 5.
But you want to ensure that cellVal.Length is = 5:
If e.ColumnIndex = 0 AndAlso cellVal.Length <> 5 Then
e.Cancel = True
MessageBox.Show(columnName & " must be 5 Digits Long!")
End If
I think you will need:
cellval.text.length