Click here to Skip to main content
13,090,930 members (47,083 online)
Rate this:
 
Please Sign up or sign in to 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 9-Jan-13 0:22am
Comments
prashant patil 4987 9-Jan-13 7:25am
   
post your code
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

whatever you assigned the DataTable that datatable having no data.
Please put a break point and check your datatable having data or not.
  Permalink  
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....
pranathis012 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
Bharath Kumar Gorle 9-Jan-13 7:37am
   
form2

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;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Rendering;

namespace Employee_Management
{
public partial class Form6 : Office2007Form

{
string combovalue;


private void Form6_Load(object sender, EventArgs e)
{

}

public Form6()
{
InitializeComponent();
}

public Form6(string str1)
{
this.combovalue = str1;
}


ViewDetails f7 = new ViewDetails();
private void GetDa()
{
//f7.fors
}
static SqlConnection con = new SqlConnection("Data Source=NAROLIS-1-PC;Initial Catalog=emp_mgmt;Persist Security Info=True;User ID=sa;Password=Narolis");
static SqlDataAdapter da = new SqlDataAdapter("select * from salary2", con);


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
if (comboBox1.SelectedIndex == 0)
{
styleManager1.ManagerStyle = eStyle.Office2007Blue;
RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(eOffice2007ColorScheme.Blue);
}
else if (comboBox1.SelectedIndex == 1)
{
styleManager1.ManagerStyle = eStyle.Office2007Silver;
RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(eOffice2007ColorScheme.Silver);
}
else if (comboBox1.SelectedIndex == 2)
{
styleManager1.ManagerStyle = eStyle.Office2007Black;
RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(eOffice2007ColorScheme.Black);
}
else
{
styleManager1.ManagerStyle = eStyle.Office2007VistaGlass;
RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(eOffice2007ColorScheme.VistaGlass);
}
}
}




#region UserMethods
#region DataBasesInGrid

public void sal(string refid)
{

string str = "select * from salary2 where refid = '" + refid + "'";
SqlConnection con = new SqlConnection("Data Source=NAROLIS-1-PC;Initial Catalog=emp_mgmt;Persist Security Info=True;User ID=sa;Password=Narolis");
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataTable dt = new DataTable();
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;
else
MessageBox.Show("Database is not pakka....");

//DataTable dt = GetDt(str);
//VerticalGrid(dt);
//return dt;
}


public void Perform(string refid)
{
string str = "select * from performance where refid = '" + refid + "'";
DataTable dt = GetDt(str);
dataGridView1.DataSource = dt;
//VerticalGrid(dt);
}



public void Daily(string refid)
{
string str = "select * from dailywork where refid = '" + refid + "'";
DataTable dt = GetDt(str);
VerticalGrid(dt);
}
#endregion


#region Grid

private void VerticalGrid(DataTable dt)
{
// Creating a table for to display in a Vertical manner
DataTable table = new DataTable();

//Get the Number of rows to make new column
for (int i = 0; i <= dt.Rows.Count; i++)
table.Columns.Add(Con
Rep
Bharath Kumar Gorle 9-Jan-13 7:13am
   
If you have any doubts ask me... i wil tell...
Salman622 15-Sep-14 6:58am
   
paste your GetDt() function
faranakm123 15-Sep-14 6:02am
   
Please help me!!!
I have this problem too.what must I do when I assign the datatable with no data to a datasource of datagridview in c#???
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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


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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 15 Sep 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100