Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        string connetionString = null;
        SqlConnection cn;
        SqlCommandBuilder cb;
        DataSet s1;
        SqlDataAdapter da;


        public Form5()
        {
            InitializeComponent();
        }
        private void form5_Load(object Sender, EventArgs e)
        {
            string connectionString = null;
            SqlConnection cn;
            connectionString = "Data Source=SHAKIR-PC\\SQLEXPRESS;Initial Catalog=UDdata;Integrated Security=True;Pooling=False";
            cn = new SqlConnection(connectionString);
            string sqlQuerry = "select * from UDdata";
            da = new SqlDataAdapter(sqlQuerry, cn);

            s1 = new DataSet();
            da.Fill(s1, "UDdata");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                cb = new SqlCommandBuilder(da);
                // to insetr the details in form to the datavase tables we need an object
                DataRow drow = s1.Tables["customer"].NewRow();
                drow[0] = textBox1.Text;
                drow[1] = textBox2.Text;
                drow[2] = textBox3.Text;

                s1.Tables["customer"].Rows.Add(drow);
                da.Update(s1, "customer");
                MessageBox.Show("Data Successfully added", "Done");

            }
            catch (Exception ee)
            {
                MessageBox.Show("There is an error " + ee.Message);
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }
    }
}

this is my code. when running my form it show "there is an error object reference not set to an instance of object" i don't no how to fix this.
Posted
Updated 18-Jul-14 2:14am
v2
Comments
[no name] 18-Jul-14 8:13am    
You debug your code, find the line that is throwing the error, find the object that is null and then make the object not null.
Atyant Srivastava 18-Jul-14 8:14am    
Please use debugger to debug the same. For where exactly it is throwing error.

First of all your dataset s1 does not have any table named Customer as per your code.

1 solution

You issue is obvious, but you haven't bothered to debug it, nor tell us which line your error is in.
I would be confident in saying that your issue is in the button1_Click event handler within your try block.

I would also be confident in saying that your issue is a very simple one which relates to you typing the wrong string.

If you debug your code (use break points) then I suspect you will quickly see the issue.

If you, I suggest you look at the documentation for the da.Fill method and what the two parameters you are passing to it represent.
Then I would examine the s1 data set after you have populated it and see what tables exist in it.
 
Share this answer
 

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



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