Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I Need a hand with this Code.
I want to create a table, from a string, the string are collected from another service / Unit.

And Display the Data on a web page:
example on how i want the website to show the data:

2 PowerFail 50
1 Restart 30
1 Alarms 60

I Need a hand whit this Code, cant se the problem With Line:
VB
Dim FinalTableResult = (From result In (From r In lstTableGroup r By New From { _


Error message says that: Error expected

Part of the New code written:
VB
            Dim array As String() = rm3.Split(ControlChars.Cr, " "c)

            Dim objTable As Table
            Dim lstTable As New List(Of Table)()
            Dim arrayCount As Integer = array.Length
            Dim count As Integer = 0

            While (count < arrayCount)
                objTable = New Table
                objTable.Text = array(count)
                count = (count + 1)
                objTable.Value = Convert.ToInt32(array(count))
                objTable.Count = 1
                lstTable.Add(objTable)
                count = (count + 1)
            End While

            Dim FinalTableResult = (From result In (From r In lstTableGroup r By New From { _
                                    r.Text, _
                                    r.Value _
                                  } New With { _
                                    Key .Text = result.Key.Text, _
                                    Key .Value = result.Key.Value, _
                                    Key .Count = result.Sum(Function(x) x.count _
                                        }) ToList()
        End While

    Finally

        con.Close()
        con.Dispose()

    End Try
End Sub


string looks like this:
PowerFail 50
Restart 30
PowerFail 50
Alarms 60

Thanks in advance!!!
Posted
Updated 4-Jul-14 11:56am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jun-14 18:40pm    
How you ended up with those strings on hands? Could you collect data in something more adequate, not string?
—SA
prototypen 26-Jun-14 14:53pm    
thanks for replying
yes I can get the string in another way, the string comms from an FTP server, and i
access the file with WebRequest. this was the only way i have found it possible to Acces the file.

C#:
C#
public class Table
  {
      public string Text { get; set; }
      public int Value { get; set; }
      public int Count { get; set; }
  }


C#
static void  Eg6()
       {
           string input = "PowerFail 50 Restart 30 PowerFail 50 Alarms 60 Alarms 60 Alarms 60 Chains 60 Lockets 60 Lockets 60 Alarms 60";
           string[] array = input.Split(' ');
           Table objTable;
           List<table> lstTable = new List<table>();
           int arrayCount = array.Length;
           int count = 0;
           while (count < arrayCount)
           {
               objTable = new Table();
               objTable.Text = array[count];
               count = count + 1;
               objTable.Value = Convert.ToInt32(array[count]);
               objTable.Count = 1;
               lstTable.Add(objTable);
               count = count + 1;
           }
        var   finalTableResult = (from r in lstTable
                               group r by new { r.Text, r.Value } into result
                               select new
                               {
                                   Text = result.Key.Text,
                                   Value = result.Key.Value,
                                   Count = result.Sum(x => x.Count)
                               }).ToList();


       }


VB:

VB
Public Class Table
	Public Property Text() As String
		Get
			Return m_Text
		End Get
		Set
			m_Text = Value
		End Set
	End Property
	Private m_Text As String
	Public Property Value() As Integer
		Get
			Return m_Value
		End Get
		Set
			m_Value = Value
		End Set
	End Property
	Private m_Value As Integer
	Public Property Count() As Integer
		Get
			Return m_Count
		End Get
		Set
			m_Count = Value
		End Set
	End Property
	Private m_Count As Integer
End Class



VB
Private Shared Sub Eg6()
	Dim input As String = "PowerFail 50 Restart 30 PowerFail 50 Alarms 60 Alarms 60 Alarms 60 Chains 60 Lockets 60 Lockets 60 Alarms 60"
	Dim array As String() = input.Split(" "C)
	Dim objTable As Table
	Dim lstTable As New List(Of Table)()
	Dim arrayCount As Integer = array.Length
	Dim count As Integer = 0
	While count < arrayCount
		objTable = New Table()
		objTable.Text = array(count)
		count = count + 1
		objTable.Value = Convert.ToInt32(array(count))
		objTable.Count = 1
		lstTable.Add(objTable)
		count = count + 1
	End While
	Dim finalTableResult = (From result In From r In lstTableGroup r By New From { _
		r.Text, _
		r.Value _
	}New With { _
		Key .Text = result.Key.Text, _
		Key .Value = result.Key.Value, _
		Key .Count = result.Sum(Function(x) x.Count) _
	}).ToList()


End Sub



finalTableResult contains the result you wanted. Comment if you need any help.
 
Share this answer
 
v3
Comments
prototypen 26-Jun-14 14:43pm    
Than you

Unfortunately I have a problem with this code. The Error is:
System.FormatException: Input string was not in a correct format

Line 101: objTable.Value = Convert.ToInt32(array(count))

I can see the change in value array and arrayCount.
PowerFail
50 Restart
30 PowerFail
50 Alarms

I think it's something with the string that I get from the FTP server.
prototypen 26-Jun-14 15:34pm    
sorry, there is a new line in the string between the value and name. copyed the debuger when i put it in the question field.

change the line to this, to get the string look like the code you write. (in the Array)

Dim array As String() = input.Split(ControlChars.Cr" "C)

Now the IstTable get the valus
(0)
Count 1
m_Count 1
m_Text PowerFail
m_Value 50
Text PowerFail
Value 50
(1)
and a lot more!

but i don`t understand the error in line 101
Prasad Avunoori 26-Jun-14 23:05pm    
Hi, Unfortunately i am not a VB.Net Coder. C# Code i posted above worked fine for me. I posted VB code after converting the C# to VB from an online converter.

VB
Imports System.Data.SqlClient
Imports System.Data
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Activities.Expressions
Imports System.Web.UI.WebControls

Partial Class Page_Default
    Inherits System.Web.UI.Page

    Public Class RegexLibrary
        Private Shared _myRegex As New Regex("(([A-Za-z]*)\s([0-9]*)\s)", (RegexOptions.Compiled Or RegexOptions.CultureInvariant))

        Public Shared ReadOnly Property MyRegex As Regex
            Get
                Return _myRegex
            End Get
        End Property
    End Class

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Try
            Dim r As Regex = RegexLibrary.MyRegex
            Dim matches As MatchCollection = r.Matches(rm3)
            Dim name As String
            For Each m As Match In matches
                name = m.Groups(0).Value
                Add(name)
            Next

            grid1.DataSource = _dict
            grid1.DataBind()

        Catch ex As Exception

        End Try

    End Sub

    Dim _dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

    Private Sub Add(ByVal name As String)
        If Not _dict.ContainsKey(name) Then
            _dict.Add(name, 1)
        Else
            _dict(name) = (_dict(name) + 1)
        End If
    End Sub

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900