Anda di halaman 1dari 4

Converting String() to Datatable

Description:

Assigining the given string value in Textbox. And on click of Submit button,splitting the

String with ‘/’ and assigning string array. Then creating the datatable with specified columns and
assigning value.

Pseudocode steps Execution

Step 1: Inside the class Employeed details , on page load of the form , datasource for datagrid is
assigned null. This method will execute everytime when the program is called as it is inside page load
event.

Page Load Description:

During load, if the current request is a postback, control properties are loaded with information
recovered from view state and control state.

Public Class EmployeeDetails


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGrid_Input.DataSource = vbNull
End Sub

Step 2:Declare a string variable and assign the text entered in text box . Split the string with ‘/’

Character and assigning to a string array.

Dim Inputvalue As String

Inputvalue = TxtInput.Text

'Split the text value using / in the string

Dim ValueExtract As String() = Inputvalue.Split(New Char() {"/"c})


Step 3:

In the below logic datatable is created with skeleton and the following columns are added .For better
coding standards the table created is assigned as datasource .And the table structure is formed.

Datatable:

In ADO.NET, DataTable objects are used to represent the tables in a DataSet. A DataTable represents
one table of in-memory relational data; the data is local to the .NET-based application in which it
resides, but can be populated from a data source such as Microsoft SQL

Is replicated in datagrid.

 Name
 Country
 Residence as

Dim table As New DataTable

Dim Name As String

Dim Country As String

Dim Residence As String

Dim Temp As String

'Clear the Datagrid value

DataGrid_Input.DataSource = table

'Declare the column header in the datatable

table.Columns.Add("Name", GetType(String))

table.Columns.Add("Country", GetType(String))

table.Columns.Add("Residence", GetType(String))
Step 4 : Iterating through ValueExtract and getting the values to table.

And finall assigning to Datagrid with the table as datasource as follows

Using for loop iterating through the values assigned to Value extract

For Each Columnvalue In ValueExtract

'Intialize the temp value as null

Temp = vbNull

'Check the counter value to add the rows in the datatable

If counter = 6 Then

Add the rows from the assigned values from variable

table.Rows.Add(Name, Country, Residence)

'Reintialize the counter value

counter = 1

End If

'Split the string again using : to extract the column value

Dim Fieldvalue As String() = Columnvalue.Split(New Char() {":"c})

'Iterate the value from the extract text

For Each value In Fieldvalue

'Check for the counter value to extract the particular column value

If (counter = 2) Then

'Assign the value to temp variable

Temp = value

'Assign the temp value to Particular column variable

Name = Temp

'Increase the counter value to avoid rechecking the same counter value

counter = counter + 1
ElseIf (counter = 4) Then

Temp = value

Country = Temp

counter = counter + 1

ElseIf (counter = 6) Then

Temp = value

Residence = Temp

End If

'Reset the counter value using value text

If (value.Contains("Name") = True) Then

counter = 2

ElseIf (value.Contains("Country") = True) Then

counter = 4

ElseIf (value.Contains("Residence") = True) Then

counter = 6

End If

Next

Next

'Assign the table value to the datasource of the grid

DataGrid_Input.DataSource = table

Catch ex As Exception

MessageBox.Show(ex.ToString())

End Try

End Sub

Anda mungkin juga menyukai