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."
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;
public FrmUserSelection()
{
InitializeComponent();
}
private void FrmUserSelection_Load(object sender, EventArgs e)
{
fillgrid();
ControlBox = false;
usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
txtwildsearch.Focus();
}
private void fillgrid()
{
ds=new DataSet();
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)
{
}
}
}
}
public static DataGridViewRow dr;
FrmUserSelection fus = new FrmUserSelection();
private void btnsearch_Click(object sender, EventArgs e)
{
DataCollection();
}
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;
}