Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why my datagridview produce duplicate data on user cell_click inside the datagridview in c#

i use the following code,but it does not pass values to the textbox in the user form fro the datagridview and give me this Error in the folowing statement;
Statement:txtusername.text=dr.Cells[0].Value.ToString();

Error:
"An unhandled exception of type 'System.NullReferenceException' occurred in Company Accounts.exe

Additional information: Object reference not set to an instance of an object."

C#
///this is my formuserslection code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace Company_Accounts
{
    public partial class FrmUserSelection : Form
    {
        DataSet ds = null; // Set null here
        public FrmUserSelection()
        {
            InitializeComponent();
           
        }
 
        private void FrmUserSelection_Load(object sender, EventArgs e)
        {
            
            fillgrid();
            ControlBox = false;
            usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            txtwildsearch.Focus();
            //this.usdgv.Columns["Password"].Visible = false;
            
        }
 
        private void fillgrid()
        {
            ds=new DataSet(); //Create new obj here
            ConnectionString.cnn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select username as [User Name],Password,Title, userrole,logincomputer as [Login Computer] ,logindate as [Login Date] ,Logintime as [Login Time] from login", ConnectionString.cnn);
            da.Fill(ds, "tbl_login");
            usdgv.DataSource = ds.Tables["tbl_login"];
            ConnectionString.cnn.Close();
        }
 
        private void usdgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            fillgrid();
            this.Close();
        }
 
        private void btnwildsearch_Click(object sender, EventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            this.Close();
        }
 
        private void usdgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            label1.Text ="" +usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s1.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s2.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s3.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
        }
 
        private void txtwildsearch_TextChanged(object sender, EventArgs e)
        {
            DataView dt = ds.Tables[0].DefaultView;
            string tbcolumn;
            try
            {
                tbcolumn = usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText.ToString();
                dt.RowFilter = string.Format("" + tbcolumn + " ='{0}'", txtwildsearch.Text);
                if (dt.Count > 0)
                {
                    usdgv.DataSource = dt; 
                }
                else
                {
                    usdgv.DataSource = 0;                    
                }
                if (usdgv.RowCount == 0)
                {
                    usdgv.DataSource = ds.Tables[0].DefaultView; 
                }
            }
            catch (Exception)
            { 
            }
        }
    }
}

////and this is the user form code.

public static DataGridViewRow dr;
        FrmUserSelection fus = new FrmUserSelection();
        private void btnsearch_Click(object sender, EventArgs e)
        {
            DataCollection();

            
        }

C#
private void DataCollection()
       {
           fus.ShowDialog();
           txtusername.Text = dr.Cells[0].Value.ToString();
           txtpassword.Text = dr.Cells[1].Value.ToString();
           txttitle.Text = dr.Cells[2].Value.ToString();
           txtuserrole.Text = dr.Cells[3].Value.ToString();
           txtlogincmpt.Text = dr.Cells[4].Value.ToString();
           txtlogindate.Text = dr.Cells[5].Value.ToString();
           txtlogintime.Text = dr.Cells[6].Value.ToString();
           disable();
           txtusername.Enabled = false;
           btnedit.Enabled = true;
           btndelete.Enabled = true;
       }
Posted
Updated 22-Nov-12 22:32pm
v2
Comments
Mohd. Mukhtar 23-Nov-12 4:33am    
On which line you are getting error?

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