VB.NET - Datatable error

Asked By Srinivasan S
08-Jun-11 02:58 AM
I have this code to get DataGridView values like 10:00am-01:00pm to the datatable,

Dim dt As New DataTable()
      Dim dr As DataRow = Nothing
      Dim spt As String = Carer.s
      Dim splitdata As String()

      splitdata = spt.Split("-")
      splitdata = spt.Split(",")
      dt.Columns.Add(New DataColumn("TimeFrom", GetType(String)))
      dt.Columns.Add(New DataColumn("TimeTo", GetType(String)))
      For i As Integer = 0 To splitdata.Length - 1
      dr = dt.NewRow()
      dr("TimeFrom") = splitdata(i).ToString()
      dr("TimeTo") = splitdata(i).ToString()
      dt.Rows.Add(dr)
      Next
      Carer.TimeGrid.DataSource = dt


But it is showing in Datatable like,

TimeFrom        TimeTo
10:00am-01:00pm    10:00am-01:00pm

I need like

TimeFrom        TimeTo
10:00am             01:00pm

Help me for this please...........
  Jon Marlow replied to Srinivasan S
08-Jun-11 04:50 AM
In your example, splitdata is going to contain the whole time range as there are no commas in the string.

If you remove the line:  splitdata = spt.Split(","), you will have the two times separated in splitdata.

To put it in the DataTable, you can do:

For i As Integer = 0 To splitdata.Length - 1 Step 2
      dr = dt.NewRow()
      dr("TimeFrom") = splitdata(i).ToString()
      dr("TimeTo") = splitdata(i + 1).ToString()
      dt.Rows.Add(dr)
Next

If you want to keep the For loop in your code. Alternatively, you can replace it with:

dr = dt.NewRow()
dr("TimeFrom") = splitdata(0).ToString()
dr("TimeTo") = splitdata(1).ToString()
dt.Rows.Add(dr)



  Kalyan Basa replied to Srinivasan S
08-Jun-11 05:11 AM
Hi Srinivasan,

dr("TimeFrom") = splitdata(i).ToString()
dr("TimeTo") = splitdata(i).ToString()

Replace the above code with the following.

dr("TimeFrom") = splitdata.Remove(splitdata.IndexOf("-"))
dr("TimeTo") = splitdata.Substring(splitdata.IndexOf("-")+1)

Hope this helps.

Thanks,
Kalyan
  Asked By Srinivasan S
08-Jun-11 05:13 AM
it is showing error on splitdata.split.

Split is not a member of System.Array

 dr("TimeFrom") = splitdata.split("-")(0).tostring()

Help me please

  Srinivasan S replied to Kalyan Basa
08-Jun-11 05:15 AM
Showing error on these two lines,

dr("TimeFrom") = splitdata.Remove(splitdata.IndexOf("-"))
dr("TimeTo") = splitdata.Substring(splitdata.IndexOf("-") + 1)

Help for this
  Asked By Srinivasan S
08-Jun-11 05:20 AM
Used this code,

 Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
        Dim spt As String = Carer.s
        Dim splitdata As String()

        'splitdata = spt.Split("-")
        splitdata = spt.Split(",")

        dt.Columns.Add(New DataColumn("TimeFrom", GetType(String)))
        dt.Columns.Add(New DataColumn("TimeTo", GetType(String)))
        For i As Integer = 0 To splitdata.Length - 1
          dr = dt.NewRow()
          dr("TimeFrom") = spt.Split("-")(0).ToString()
          dr("TimeTo") = spt.Split("-")(1).ToString()
          dt.Rows.Add(dr)
        Next
        Carer.TimeGrid.DataSource = dt
        Carer.TimeGrid.Columns(0).Width = 180
        Carer.TimeGrid.Columns(1).Width = 180

Datatable gives like,

TimeFrom              TimeTo
10:00am             01:00pm,02:00pm
10:00am              01:00pm,02:00pm

I need like,

TimeFrom              TimeTo
10:00am             01:00pm
02:00pm                       05:00pm      

Help me     
  Srinivasan S replied to Jon Marlow
08-Jun-11 05:25 AM
used your code. But am not getting my result. Help me.
  Jon Marlow replied to Srinivasan S
08-Jun-11 05:36 AM
Try the following:

       Dim dt As New DataTable()
    Dim dr As DataRow = Nothing
    Dim spt As String = "10:00am-01:00pm,02:00am-04:00pm"
    Dim splitdata As String()
    Dim splitdata2 As String()
    dt.Columns.Add(New DataColumn("TimeFrom", GetType(String)))
    dt.Columns.Add(New DataColumn("TimeTo", GetType(String)))
 
    splitdata = spt.Split(",")
    For i As Integer = 0 To splitdata.Length - 1
      splitdata2 = splitdata(i).Split("-")
      dr = dt.NewRow()
      dr("TimeFrom") = splitdata2(0).ToString()
      dr("TimeTo") = splitdata2(1).ToString()
      dt.Rows.Add(dr)
    Next
  Srinivasan S replied to Jon Marlow
08-Jun-11 05:58 AM
REally you are Great sir.
Create New Account
help
Datagridview to Datatable How can i get datagridview particular cell values to datatable in vb.net? DataTable dynamicTable = new DataTable(); DataRow dynamicRow; DataColumn dcMacAddress = new DataColumn("MacAddress", typeof(string)); DataColumn Serial = new DataColumn("Serial Number", typeof(string)); dynamicTable.Columns.Add(dcMacAddress); dynamicTable.Columns.Add(Serial
in advance srinu Hello, You can create dynamic table as follow / / Create a object of Datatable DataTable dynamicTable = new DataTable(); / / DataRow DataRow dynamicRow; / / DataColumn for Name and Time DataColumn NameColumn = new DataColumn( "Name" , typeof ( string )); DataColumn TimeColumn = new DataColumn( "Time" , typeof ( string )); / / add Column to Datatable dynamicTable
ADO.NET i have a datatable DataTable dtdest = new DataTable (); foreach ( string curname in TableColumnName) { DataColumn coldest = new DataColumn (curname); dtdest.Columns.Add(coldest); } foreach ( DataRow currow in dt.Rows) { DataRow newrow = dtdest.NewRow(); foreach ( DataColumn curcol in dtdest.Columns) { newrow[curcol.Caption] = currow[curcol.Caption]; } dtdest.Rows.Add(newrow); i
insert data into SQL 2005 from a text file that I parse. I build a DataTable: [CODE] public DataTable tbl = new DataTable(); public DataTable buildType4DataTable() { DataColumn loadTransactionCode = new DataColumn(); loadTransactionCode.ColumnName = "loadTransactionCode"; tbl.Columns.Add(loadTransactionCode); DataColumn companyID = new DataColumn(); companyID.ColumnName = "companyID"; tbl.Columns.Add(companyID); DataColumn cardholderIdentification = new DataColumn(); cardholderIdentification.ColumnName = "cardholderIdentification"; tbl
ADO.NET and C# i have a datatable DataTable dataTable = new DataTable (); in this datatable i stored the data from a textfile the text file contents are StudentName, StudentId, StudentGrade 3 , B i separated each word by , (comma) and i stored them to the above datatable then i created a database table with columns names StudentId, StudentName, StudentGrade the datatype of the datatypes are varchar now i need to check the datatatype of each word in datatable or text file and compare with the datatype of database table, if the datatypes are use following code. / / I have done this on Click of button it ll first create datatable in your format fill the data and than it ll validate. DataTable dt = new DataTable