none
Finding single record to datagrid in C# RRS feed

  • Question

  • Here I endorsed code for finding record but it's not working properly.

    SqlDataAdapter Da = new SqlDataAdapter();
            DataSet Ds = new DataSet();
            DataTable Dt = new DataTable();
            DataRowView Dv;
            CurrencyManager cmgr;

    private void frmUser_Load(object sender, EventArgs e) { SqlConnection Conn = new SqlConnection(); Conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=UserLogin;Integrated Security=True"; dgRecord.Visible = false; Da = new SqlDataAdapter("select * from signup", Conn); Ds = new DataSet(); Da.Fill(Ds); dgRecord.DataSource = Ds.Tables[0]; cmgr=(CurrencyManager)dgRecord.BindingContext[Ds.Tables[0]]; } private void btnFind_Click(object sender, EventArgs e) { try { DataView Dview = new DataView(Ds.Tables[0]); Dview.Sort = "UserName"; int i = Dview.Find(txtFind.Text); if (i < 0) { MessageBox.Show("Sorry !! Finding Records Not Found"); txtFind.Text = ""; txtFind.Select(); } else { cmgr.Position = i; Dv=(DataRowView)cmgr.Current; txtUser.Text = Dv.Row[0].ToString(); txtPassword.Text = Dv.Row[1].ToString(); txtCPassword.Text = Dv.Row[2].ToString(); } } catch (Exception) { MessageBox.Show("Check Your Input Data.........."); txtFind.Clear(); txtFind.Focus(); } }



    Wednesday, October 17, 2012 6:30 AM

Answers

  • DataView.Find() will find the text on primary Key column. Is UserName is your primary key column in database? If yes it should work fine.

    A preferable solution is to use DataView.RowFilter method. Below code snippet should work fine for you.

    DataView Dview = new DataView(Ds.Tables[0]);
    Dview.RowFilter = 
    "UserName=" + "'" + txtFind.Text.ToString() + "'";
    if (Dview.Count == 0)
    {
        MessageBox.Show("Sorry !! Finding Records Not Found");
        txtFind.Text = "";
        txtFind.Select();
    }
    else
    {
        txtUser.Text = Dview[0][0].ToString();
        txtPassword.Text = Dview[0][1].ToString();
        txtCPassword.Text = Dview[0][2].ToString();
    }


    Lingaraj Mishra

    Wednesday, October 17, 2012 8:38 AM

All replies

  • DataView.Find() will find the text on primary Key column. Is UserName is your primary key column in database? If yes it should work fine.

    A preferable solution is to use DataView.RowFilter method. Below code snippet should work fine for you.

    DataView Dview = new DataView(Ds.Tables[0]);
    Dview.RowFilter = 
    "UserName=" + "'" + txtFind.Text.ToString() + "'";
    if (Dview.Count == 0)
    {
        MessageBox.Show("Sorry !! Finding Records Not Found");
        txtFind.Text = "";
        txtFind.Select();
    }
    else
    {
        txtUser.Text = Dview[0][0].ToString();
        txtPassword.Text = Dview[0][1].ToString();
        txtCPassword.Text = Dview[0][2].ToString();
    }


    Lingaraj Mishra

    Wednesday, October 17, 2012 8:38 AM
  • Hi Mishra

    It's Working. Thanks for co-operation and continue connect with me. Waiting for next help !. Thanks Again.

    Wednesday, October 17, 2012 9:47 AM
  • Glad to see your issue is resolved.

    Lingaraj Mishra

    Wednesday, October 17, 2012 9:56 AM