Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my code every thing is good...When I am executing this statement it showing the error

Object reference not set to an instance of an object.

and showing an exception NullReferenceException....

please help me...
Thanks in advance....
Posted
Comments
prashant patil 4987 9-Jan-13 7:25am    
post your code

Hi,

whatever you assigned the DataTable that datatable having no data.
Please put a break point and check your datatable having data or not.
 
Share this answer
 
Comments
Bharath Kumar Gorle 9-Jan-13 6:27am    
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;


i used this code..even i get thet error...
Yugal Pandya 9-Jan-13 7:24am    
try This

if (dt!=null)
{
if(dt.rows.count>0)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = dt;
}
}
Bharath Kumar Gorle 9-Jan-13 7:30am    
Even though it is showing the same error....
[no name] 9-Jan-13 6:40am    
please send me your code
Bharath Kumar Gorle 9-Jan-13 7:37am    
form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


namespace Employee_Management
{
public partial class ViewDetails : DevComponents.DotNetBar.Metro.MetroForm
{

byte[] byteImg;
public ViewDetails()
{
InitializeComponent();
}

DataSet ds = new DataSet();

private void Form7_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'emp_mgmtDataSet1.employee' table. You can move, or remove it, as needed.
this.employeeTableAdapter1.Fill(this.emp_mgmtDataSet1.employee);

// TODO: This line of code loads data into the 'emp_mgmtDataSet.employee' table. You can move, or remove it, as needed.
this.employeeTableAdapter.Fill(this.emp_mgmtDataSet.employee);
foreach (var ctrl in this.Controls)
if (ctrl is TextBox)
(ctrl as TextBox).ReadOnly = true;
}



#region Button_Click_Events
private void button1_Click(object sender, EventArgs e)
{
button3.Visible = true;
textBox14.Visible = true;
foreach (var ctrl in this.Controls)
{
if (ctrl is TextBox)
(ctrl as TextBox).ReadOnly = false;
}

}

private void button2_Click(object sender, EventArgs e)
{
string str = "update employee set fname ='" + textBox1.Text + "',lname ='" + textBox2.Text + "' ,email = '" + textBox4.Text + "',mobile = '" + textBox5.Text + "',college ='" + textBox6.Text + "',collegelocation ='" + textBox7.Text + "',fathername ='" + textBox8.Text + "',nationality='" + textBox9.Text + "',dob='" + textBox10.Text + "',gender ='" + textBox11.Text + "',currentaddress = '" + textBox12.Text + "',permanentaddress ='" + textBox13.Text + "',photo= @photo,experience ='" + textBox15.Text + "',currentindustry='" + textBox16.Text + "',domain = '" + textBox17.Text + "',keyskills ='" + textBox18.Text + "' where refid = '" + comboBox1.SelectedValue +"'";
SqlConnection con = new SqlConnection("Data Source=NAROLIS-1-PC;Initial Catalog=emp_mgmt;Persist Security Info=True;User ID=sa;Password=Narolis");
SqlCommand cmd = new SqlCommand(str, con);
byte[] byteImg;

if (pictureBox1.Image != null)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byteImg = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteImg, 0, byteImg.Length);
cmd.Parameters.AddWithValue("@photo", byteImg);
}

con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();

if (n > 0)
{
MessageBox.Show("The employee " + comboBox1.SelectedValue + "Hasbeen Modified");
}
else
MessageBox.Show("Upadation failed");

txtclear();
}



private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(open.FileName);
textBox14.Text = open.FileName;
}
}

private void button4_Click(object sender, EventArgs e)
{
string str = comboBox1.SelectedValue.ToString();
//Form6 f6 = n
C#
private void button5_Click(object sender, EventArgs e)
        {
           string str = comboBox1.SelectedValue.ToString();            
            Form6 f6 = new Form6();
            f6.Perform(str);
            f6.Show();           
        }


When i Used this Code it is not having any error....


C#
private void button5_Click(object sender, EventArgs e)
        {
           string str = comboBox1.SelectedValue.ToString();
            Form6 f6 = new Form6(str);
            f6.Perform(str);
            f6.Show();
        }
</pre>


When i used this code it showing the error....
here perform() display a daatabase in form6
 
Share this answer
 
v2
Comments
Sanjeev Alamuri 9-Jan-13 6:39am    
There nothing difference in Both code blocks. so how can we expect no error raising in firstblock and error in another?
Bharath Kumar Gorle 9-Jan-13 6:41am    
Ya bro..thats what i was confused..why this error...
Bharath Kumar Gorle 9-Jan-13 6:44am    
If you want to see... I will give my team viewer passowrd...

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