Loop For Datatable
-
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
-
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
-
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.
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
-
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
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 )
-
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 )