Click here to Skip to main content
15,890,357 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking for help on how to display results when searching for clients informations with their “surname” on my application
I want to display a new form with multiple results in a List View (clientid, surname, othername) and allow the user to select the one they are looking for

codes in class
public bool searchpersonDetails(string personname)
	    {
           if (surname == string.Empty) 
                {
                    Fom1 frm = new Fom1(personname);
		    frm.Show();
		    personname = surname.ToString();

	        }

            SqlCommand cmd = new SqlCommand();
	    string sqlQuery = null;

            sqlQuery = "select client_id, surname, othername from tblspersonaldetails where surname like '%" + personname + "%';";

            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
	    cmd.CommandType = System.Data.CommandType.Text;

            SqlDataReader dr = null;
	    dr = cmd.ExecuteReader();
	if (dr.Read()) 
            {
                
		client_id = dr["client_id"].ToString();
                surname = dr["surname"].ToString();
                othername = dr["othername"].ToString();

                return true;
            }

            else
            {
                return false;
            }

        }

    }
}



codes behind button click


C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                getid getidentity = new getid();

                if (getidentity.searchpersonDetails(txtSurname.Text))
                {

                    var
                    _with9 = this;

                    _with9.txtSurname.Text = getidentity.Sname.ToString();
                }

                else
                {
                    MessageBox.Show("Record not found");
                }
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                // Close data reader object and database connection


                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }

        }
    }
}



codes behind form

public partial class Fom1 : Form
    {

            SqlConnection conn = new SqlConnection();

            public Fom1(string personname)
            {
                InitializeComponent();

                // Add any initialization after the InitializeComponent() call.
                SqlCommand cmd = new SqlCommand();


                string sqlQuery = null;
                //this criteria As String = personName & "%"
                sqlQuery = "select * from tblspersonaldetails where surname like '" + personname + "%" + "' order by surname,othername asc";
                cmd.Connection = conn;
                cmd.CommandText = sqlQuery;
                cmd.CommandType = System.Data.CommandType.Text;
                SqlDataReader dr = null;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ListViewItem lst = default(ListViewItem);

                    lst = this.listView1.Items.Add(dr["client_id"].ToString());
                    lst.SubItems.Add(dr["surname"].ToString());
                    lst.SubItems.Add(dr["othername"].ToString());
                }
                cmd.Dispose();

            }



BUT IT IS STILL NOT DISPLAYING ON THE FORM PLease HELP ME OUT
Posted
Updated 8-Jan-12 5:14am
v6
Comments
Wendelius 29-Dec-11 10:09am    
What part is the problem, creating the query, using the controls etc?
mikeoabban 30-Dec-11 14:31pm    
i just dont understand why the result is not displaying at all on the form
Uday P.Singh 29-Dec-11 10:26am    
where are you getting stuck?
mikeoabban 30-Dec-11 14:33pm    
the result isnot displaying clientid, surname, othername on the form
RaviRanjanKr 29-Dec-11 13:19pm    
[Edited]Code is wrapped in pre tag[/Edited]

This isn't difficult, but it is hard to give you code because we don't know how you do things. But the process itself is pretty simple:

1) Retrieve the info from the database.
2) Package it into a suitable structure - a List<T>, or a DataTable (if you use the later, it can be combined with (1) above very easily)
3) Create your new form and add your structure to teh default constructor. So if for example you do use a DataTable, the default constructor gets a single parameter - the DataTable.
4) Add a public property (a getter only) which returns the selected info - id, DataRow, or element of List that you handed in the constructor
5) Add the info to your grid and supply an OK and Cancel button.

Done!
To use it, create an instance of the new form, passing it the info. Call the ShowDialog method of the form instance, and check for OK return. If you get it, get the selected info from the property.
 
Share this answer
 
Comments
Andrew Piotrowski 29-Dec-11 10:39am    
Also you can do this from an XML file, without going to databases. You can use System.XML and System.Serialize. It's only advice - this solution is good.
Wendelius 29-Dec-11 10:40am    
Well explained, my 5.
RaviRanjanKr 29-Dec-11 13:21pm    
My 5+
Based on your latest comment, there seems to be no code that's actually putting the results in the form. Should you have something like:
...
C#
TextBoxSurName.Text = dr["surname"].ToString();
...
 
Share this answer
 
Comments
mikeoabban 4-Jan-12 13:07pm    
am having this in the class surname = dr["surname"].ToString();
this behind the button _with9.txtSurname.Text = getidentity.Sname.ToString();

Pls where should i put this TextBoxSurName.Text = dr["surname"].ToString();
and how. am confused
Wendelius 4-Jan-12 13:11pm    
What I meant is that I don't see the code actually putting the content of dr["surname"] to a control in the window. It looks like you assign the value to a variable but is it also set to a control somewhere?
mikeoabban 4-Jan-12 14:10pm    
Ok that is i have write a code to put the content of dr["surname"] to the new form (which is Fom1 )
if so then i will do my best to write the codes though this is my first c#
program
mikeoabban 4-Jan-12 13:41pm    
i sometimes get this error message
ExecuteReader requires an open and available Connection. The connection's current state is closed
Wendelius 4-Jan-12 13:54pm    
Then you try to use the reader but yo uhave closed the connection. There's no conn.Open() visible in your code so probably you open/close the connection somewhere else. I'd suggest putting a breakpoint on the line where you close the connectionand see when it hit's that breakpoint.

Another possibility is to change the design so that you always open the connection, execute reader, close connection. This way the connectionwould be open only when something is done against the database.
use code like
C#
public datatable search(string sirname) //in your class
{
   datatable dt=new datatable();
   new sqldataadaptor("query",con).fill(dt);
   return dt;
}

on btn click
C#
datatable dt=classobject.search(txtsirname); //input by user
 datagrid.datasource=dt; //bind datagrid here
 
Share this answer
 
v2

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