Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Loop For Datatable

Loop For Datatable

Scheduled Pinned Locked Moved Visual Basic
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ejaz_pk
    wrote on last edited by
    #1

    I am using datatable in my application to show the data in gridview.Now what I want is that when user enter a new line application should check the datatable.if the record exists then it should show a warning otherwise add a new row in datatable. I am using the following code to check the datatable through loop. Dim x As DataRow For Each x In mydatatable.Rows If x.Item(1).ToString = Me.TextBox1.Text Then Me.Label1.Text = "Exist" End If Next With the above code I am getting error 'Object reference not set to an instance of an object.' at the first row of the for loop Ejaz Ejaz

    _ C 2 Replies Last reply
    0
    • E ejaz_pk

      I am using datatable in my application to show the data in gridview.Now what I want is that when user enter a new line application should check the datatable.if the record exists then it should show a warning otherwise add a new row in datatable. I am using the following code to check the datatable through loop. Dim x As DataRow For Each x In mydatatable.Rows If x.Item(1).ToString = Me.TextBox1.Text Then Me.Label1.Text = "Exist" End If Next With the above code I am getting error 'Object reference not set to an instance of an object.' at the first row of the for loop Ejaz Ejaz

      _ Offline
      _ Offline
      _mubashir
      wrote on last edited by
      #2

      ejaz_pk wrote:

      If x.Item(1).ToString = Me.TextBox1.Text Then

      I think this line is causing the issue. Does your datatable has column 1?


      Mubashir Software Architect Storan Technologies Inc, USA Every job is a self portrait of the person who did it.

      E 1 Reply Last reply
      0
      • _ _mubashir

        ejaz_pk wrote:

        If x.Item(1).ToString = Me.TextBox1.Text Then

        I think this line is causing the issue. Does your datatable has column 1?


        Mubashir Software Architect Storan Technologies Inc, USA Every job is a self portrait of the person who did it.

        E Offline
        E Offline
        ejaz_pk
        wrote on last edited by
        #3

        Here is coding for the application I am getting error message when click on button1_click event Imports System.Data Imports System.Data.OleDb Partial Class _Default Inherits System.Web.UI.Page Dim MYDT As DataTable Dim mydatatable As DataTable Private Function CreateDataTable() As DataTable mydatatable = New DataTable() Dim myDataColumn As DataColumn myDataColumn = New DataColumn() myDataColumn.DataType = Type.GetType("System.String") myDataColumn.ColumnName = "id" myDataTable.Columns.Add(myDataColumn) myDataColumn = New DataColumn() myDataColumn.DataType = Type.GetType("System.String") myDataColumn.ColumnName = "username" myDataTable.Columns.Add(myDataColumn) myDataColumn = New DataColumn() myDataColumn.DataType = Type.GetType("System.String") myDataColumn.ColumnName = "firstname" myDataTable.Columns.Add(myDataColumn) myDataColumn = New DataColumn() myDataColumn.DataType = Type.GetType("System.String") myDataColumn.ColumnName = "lastname" myDataTable.Columns.Add(myDataColumn) Return myDataTable End Function Private Sub AddDataToTable(ByVal username As String, ByVal firstname As String, ByVal lastname As String, ByVal myTable As DataTable) Dim row As DataRow row = myTable.NewRow() row("id") = Guid.NewGuid().ToString() row("username") = username row("firstname") = firstname row("lastname") = lastname myTable.Rows.Add(row) End Sub Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load If Me.IsPostBack = False Then MYDT = New Data.DataTable() MYDT = CreateDataTable() Session("myDatatable") = MYDT Me.GridView1.DataSource = (CType(Session("myDatatable"), Data.DataTable)).DefaultView Me.GridView1.DataBind() End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click If Me.TextBox1.Text.Trim() = "" Then Me.Label1.Text = "You must fill a username." Return Else Dim x As DataRow For Each x In mydatatable.Rows If x.Item(1).ToString = Me.TextBox1.Text Then Me.Label1.Text = "Exist" End If

        1 Reply Last reply
        0
        • E ejaz_pk

          I am using datatable in my application to show the data in gridview.Now what I want is that when user enter a new line application should check the datatable.if the record exists then it should show a warning otherwise add a new row in datatable. I am using the following code to check the datatable through loop. Dim x As DataRow For Each x In mydatatable.Rows If x.Item(1).ToString = Me.TextBox1.Text Then Me.Label1.Text = "Exist" End If Next With the above code I am getting error 'Object reference not set to an instance of an object.' at the first row of the for loop Ejaz Ejaz

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You have several issues here 1 - you're giving all your variables names that obsfucate your code. Give them real names 2 - you need to learn how to use the debugger, you should set a break point or two in your code, and examine your data source to see what's going wrong 3 - you have two datatables as members, called MYDT and myDataTable ( or something ). More bad naming 4 - SQL is what you use to filter a data source. What you're doing is not the right way to go about filtering data.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          E 1 Reply Last reply
          0
          • C Christian Graus

            You have several issues here 1 - you're giving all your variables names that obsfucate your code. Give them real names 2 - you need to learn how to use the debugger, you should set a break point or two in your code, and examine your data source to see what's going wrong 3 - you have two datatables as members, called MYDT and myDataTable ( or something ). More bad naming 4 - SQL is what you use to filter a data source. What you're doing is not the right way to go about filtering data.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            E Offline
            E Offline
            ejaz_pk
            wrote on last edited by
            #5

            Thanks Christian for reply. Actually this is my test code and not using in the application. This is my first test by using datatable in asp.net

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups