-1

i am new to c#.net i found the following code in the web and i modified it but its not working

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.SqlClient;

namespace InsertUpdateDeleteDataGridView
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\bj\documents\visual studio 2013\Projects\InsertUpdateDeleteDataGridView\InsertUpdateDeleteDataGridView\Information.mdf;Integrated Security=True");
        SqlCommand cmd;
        SqlDataAdapter adapt;
        //ID variable used in Updating and Deleting Record
        int id = 0;




        public Form1()
        {
            InitializeComponent();
            //invok fn
            DisplayData();
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
           if(txtbxname.Text!="" && txtbxcountry.Text!="")
          {
            cmd = new SqlCommand("INSERT INTO users(name,country) VALUES(@name,@country)",con);
            con.Open();
            cmd.Parameters.AddWithValue("@name", txtbxname.Text);
            cmd.Parameters.AddWithValue("@country", txtbxcountry.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record added succesfully","Success");
            DisplayData();
            ClearData();
           }
            else
             {
            MessageBox.Show("please provide Details!","Error");
             }

        }
           //displaying Data in DataGridView

        private void DisplayData()
        {
          con.Open();
            //creating obj of datatable method
            DataTable dt= new DataTable();
            dt = null;
            adapt.Fill(dt);
            dataGridView1.DataSource=dt;
            con.Close();
        }
        //clearing datat
        private void ClearData()
        {
            txtbxname.Text="";
            txtbxcountry.Text="";
            id=0;
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
          id=Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            txtbxname.Text=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtbxcountry.Text=dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
        }
         //update record
        private void btnupdate_Click(object sender, EventArgs e) 
        {  
            if(txtbxname.Text!="" && txtbxcountry.Text!="")
            {

            cmd=new SqlCommand("UPDATE users SET name=@name,state=@state WHERE id=@id ",con);
            con.Open();
            cmd.Parameters.AddWithValue("@id",id);
            cmd.Parameters.AddWithValue("@name",txtbxname.Text);
            cmd.Parameters.AddWithValue("@country",txtbxcountry.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("record updated succesfully","success");
            con.Close();
                DisplayData();
            ClearData();
           }
            else
            {
             MessageBox.Show("please select the record to update!","erorrr!!");
            }
         }
        //deleterecord
        private void btndelete_Click(object sender, EventArgs e)
        { 
              if(txtbxname.Text!="" && txtbxcountry.Text!="")
            {
            cmd=new SqlCommand("DELETE students WHERE id=@id",con);
            con.Open();
            cmd.Parameters.AddWithValue("@id",id);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record deleted successfully!");
            DisplayData();
            ClearData();

        }
        else
        {
            MessageBox.Show("please select record to delete","error");
         }


     }
   }

 }

but when ever i try to run there is exception called in this line

adapt.filldata(dt);

  private void DisplayData()
        {
          con.Open();
            //creating obj of datatable method
            DataTable dt= new DataTable();
            dt=null;
            adapt = new SqlDataAdapter("Select *from users ", con);
            adapt.Fill(dt);
            dataGridView1.DataSource=dt;
            con.Close();
        }

i again modified my creating object but the result is ame

i searched a lot but didnot got any answer so i am got stucked is there any one to help me

1 Answers1

1

The adapt object has not been instantiated. You need to to create the object first.

this article gives a simple example of how to create the adapter and then call the fill method. you can not call methods on objects before creating them.

https://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.110).aspx

n00b
  • 1,822
  • 14
  • 25