C# winforms, how to show records in datagridview if there is more than one record from matching text field

Hi guys,

I am new to C#. I am experienced with VBA, though that really does not have that much to do with this..

I have a datagridview on my winform, that I want to only show with data if there is more than one record existing with the CallSign that
is currently on the form itself.

This is the code I have for the datagridview
***************************
 OleDbCommand command = new OleDbCommand();
            connection2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\HamLogBookV10\hamlogbook.accdb;
Persist Security Info=False;";
            command.Connection = connection2;

            string query2 = "select CallSign, CallDate, FullName, [Band], Freq, Mode, City, State, Country From Main WHERE (CallSign = '"+txtCallSign.Text+"')";
           
            command.CommandText = query2;
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dz = new DataTable();
                da.Fill(dz);
                 mainDataGridView1.DataSource = dz;
****************************
so, what I want to happen is, on the form above this datagridview, are all the text fields.
the one field that I am interested in is  txtCallSign, so if it already has 1 or more existing records in the database file
with the same callsign, then I want to populate the datagridview with those records, otherwise, the datagridview does
not need to be populated.

thanks very much,
Kevin Rea
Lancaster, Calif.
Kevin ReaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srikanthreddyn143Commented:
Have a Condition before assigning daratable
.

If (dz!=null && dz.rows.count>0)
mainDataGridView1.DataSource = dz;
else
mainDataGridView1.DataSource = null;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikanthreddyn143Commented:
This way you are populating grid only if there is data returned from database.
0
Eric GreeneDirector of TechnologyCommented:
I would do as srikanthreddyn143 said, and I'd cleanup the code a bit with the "using" directive.  It allows your objects to be destroyed without a lot of extra code.  Here is how I would do it:
            //By creating the dz and query2 variables here, they are available to all the code within your procedure.
            DataTable dz = new DataTable();
            string query2 = "select CallSign, CallDate, FullName, [Band], Freq, Mode, City, State, Country From Main WHERE (CallSign = '+txtCallSign.Text+')";

            //Here you would use a "using" directive -- this is similar to the VB "with" command
            using(OleDbConnection connection2 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\HamLogBookV10\\hamlogbook.accdb;Persist Security Info=False;"))
            {
                //Here, you want to try your connection first.  You don't want an unexpected error popping-up on you with your users
                try
                {
                    connection2.Open();
                }
                catch (OleDbException ex)
                {
                    string error = "do something here where you would report the error:\n\r" + ex.Message;
                    return;
                }

                //This is a nested using directive for the command object.
                using(OleDbCommand command = new OleDbCommand(query2,connection2))
                {
                    command.CommandType = CommandType.Text;
                    OleDbDataAdapter da = new OleDbDataAdapter(command);

                    try
                    {
                        da.Fill(dz);
                    }
                    catch (Exception ex)
                    {
                        string error = "do something here where you would report the error:\n\r" + ex.Message;
                        return;
                    }
                }

                //This was srikanthreddyn143's suggestion here
                if (dz != null && dz.Rows.Count > 0)
                {
                    mainDataGridView1.DataSource = dz;
                }
            }

Open in new window


Keep in mind that this is the exact same solution srikanthreddyn143 gave you, just with code and some good practice cleanup.  If this turns out to work for you, give the points to srikanthreddyn143
0
Eric GreeneDirector of TechnologyCommented:
I think the points should all be given to srikanthreddyn143.  That poster was first to answer the actual question.  I only added some management to the code the OP posted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.