Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
Hello Guys, i am facing a similar problem i don't know why the problem is that when i close and check if data table is null so do nothing else i want to show value on text box but it's shows value on textbox but when data table is null so it's gonna be stuck or close form automatically.
please guys help me and give me the solution of this problem!

What I have tried:

C#
DataSet dsa = new DataSet();
            DataTable dt = new DataTable();
            dsa.Tables.Add(dt);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da = new OleDbDataAdapter("SELECT * FROM [Product Management] where [Product ID] = " + comboBox1.Text + "", connection);
            
            if (dsa != null)
            {
                da.Fill(dt);
                textBox2.Text = dt.Rows[0]["Product Name"].ToString();
                
            }
            else
            {
                MessageBox.Show("Hello World","Combobox1",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
Posted
Updated 7-Dec-16 15:33pm
Comments
[no name] 7-Dec-16 9:34am    
"a similar problem", a similar problem to what?

Your code does not match your description. You aren't checking any data table for null. You are checking the data set for being null which it never would be.
Member 9983063 7-Dec-16 9:50am    
sir please tell me where is the mistake in my code

Do two things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Use the debugger to follow exactly what is happening with your code. Put a breakpoint at the beginning of the method, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values. At some point it should be obvious where it is startign to go wrong, and you can begin looking back to find out why.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Hi Member 9983063, the following line is not checking for an empty table.
C#
if (dsa != null)

And it is never NULL as an object is assigned to it; a memory is allocated.
C#
DataSet dsa = new DataSet();

So the conditional check is actually not checking for what you need.

What will be best is check the number of rows returned in the datatable.
C#
//if (dsa != null)
//{
//    da.Fill(dt);
//    textBox2.Text = dt.Rows[0]["Product Name"].ToString();
//}

da.Fill(dt);
if (dt.Rows.Count > 0)
{
    textBox2.Text = dt.Rows[0]["Product Name"].ToString();
}
else
{
    MessageBox.Show("Hello World", "Combobox1", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

The dataset is not used in this portion. If it is not used otherwise you can remove it.

Also mind the first point from OriginalGriff. This is very crucial not to send the query string over the network. For small scale intra-network applications this might be no problem, but for large-scale enterprise applications this is a very important trapdoor. So it is better to stand on the practice from the very beginning.
 
Share this answer
 

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