Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Validation_Project
{
    public partial class Form1 : Form
    {
               
                DataRow    drow;
                DataTable  dtable;
               String formid;
        int flag;
        
               

        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {
            DataColumn[] keycolumns = new DataColumn[1];
            keycolumns[0] = dtable.Columns["formid"];
            dtable.PrimaryKey = keycolumns;



            dtable = dataSet11.Tables["WCD"];
             drow = dtable.Rows.Find(formid);
             drow.BeginEdit();
        
            drow[0] = txtvalidation.Text;
           
            drow.EndEdit();
           
            txtformid.Enabled = true;
            oleDbDataAdapter1.Update(dataSet11);
            oleDbDataAdapter1.Fill(dataSet11);
            MessageBox.Show("Record Submited.......");
         
            
        }
       
        private void btnok_Click(object sender, EventArgs e)
        {
            oleDbDataAdapter1.SelectCommand.Parameters["formid"].Value = txtformid.Text;
            oleDbDataAdapter1.Fill(dataSet11);

        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            dataSet11.Clear();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtvalidation.Text == "INVALID")
            {
                groupBox2.Visible = true;
            }
            else
            {
                groupBox2.Visible = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
       
        }

       
    }
}
Posted
Updated 8-Jun-12 0:46am
v3
Comments
VJ Reddy 8-Jun-12 6:10am    
Edit: pre tag for C# code added.
VJ Reddy 16-Jun-12 2:06am    
Thank you for viewing and accepting the solution :)

1 solution

As seen from the following code
C#
DataColumn[] keycolumns = new DataColumn[1];
keycolumns[0] = dtable.Columns["formid"];
dtable.PrimaryKey = keycolumns;

dtable = dataSet11.Tables["WCD"];
drow = dtable.Rows.Find(formid);


The dtable.Columns is accessed before a DataTable is assigned to the variable dtable. Hence, the dtable is null and accessing a property on null object will result in the above stated error. Modifying the code as below by placing dtable = dataSet1. at beginning may solve the problem
C#
private void btnsubmit_Click(object sender, EventArgs e)
{
    dtable = dataSet11.Tables["WCD"];   
    DataColumn[] keycolumns = new DataColumn[1];
    keycolumns[0] = dtable.Columns["formid"];
    dtable.PrimaryKey = keycolumns;
    
    drow = dtable.Rows.Find(formid);
 
Share this answer
 
Comments
codeBegin 8-Jun-12 6:25am    
my 5!
VJ Reddy 8-Jun-12 6:35am    
Thank you, codeBegin :)
Rahul Rajat Singh 8-Jun-12 6:27am    
good answer. +5 from me.
VJ Reddy 8-Jun-12 6:35am    
Thank you, Rahul :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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