Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello Guys, i am selecting value from database but datatable return null values

What I have tried:

connection.Open();
            DataSet dsa1 = new DataSet();
            DataTable dt1 = new DataTable();
            dsa1.Tables.Add(dt1);
            OleDbDataAdapter da1 = new OleDbDataAdapter();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                da1 = new OleDbDataAdapter("SELECT [Price] from [Purchase Record] where [Product Name] = '" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'", connection);
                da1.Fill(dt1);
                dataGridView2.DataSource = dt1;
                connection.Close();
            }
Posted
Updated 3-Jan-17 9:24am
Comments
David_Wimbley 2-Jan-17 20:34pm    
So what issue are you encountering with the null values from your DB?

Without much information, a simple solution would be to run update statements on your DB to prevent any null columns from being returned. EX: If Price IS NULL set Price == 0.

Another solution would be, based on the fact you are using an embedded query, use ISNULL([Price], 0) instead of just [Price].

Then the other thought is that your product name doesn't exist in your DB which is why you get nulls because you can't select something that doesn't exist.

See how not having enough information about your problem isn't very useful?
Member 9983063 2-Jan-17 20:38pm    
my problem is i have 2 table in my database one is sale and another is purchase i want to show purchase price in sale form in c#
Arunprasath Natarajan 3-Jan-17 2:49am    
Have you tried ADD

There is logical error.
1. You are Filling in a loop so after every fill it will wipe previous filled data. (Data is replaced not appended.)
2. connection is closed in loop so after first iteration the table object will be null.
 
Share this answer
 
Comments
Maciej Los 3-Jan-17 2:07am    
Good catch!
5.
Member 9983063 3-Jan-17 5:01am    
but i need datagridview all rows value i think it's can be done with loop if not so please suggest me how can i do this??
your code is modified.. use this
this may be work for u

connection.Open();
            DataSet dsa1 = new DataSet();
            DataTable dt1 = new DataTable();
            dsa1.Tables.Add(dt1);
            OleDbDataAdapter da1 = new OleDbDataAdapter();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                da1 = new OleDbDataAdapter("SELECT [Price] from [Purchase Record] where [Product Name] = '" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'", connection);
                da1.Fill(dt1);
                int y=dataGridView1.Rows.Add();
                dataGridView1.Rows[y].Cells[0].Value=dt1.Rows[0].Field<double>(0);
                dt1.Clear();
                connection.Close();
            }
 
Share this answer
 
I'm not sure if I understand the situation correctly, but...

As already pointed out, don't close the connection inside the loop, otherwise the second fetch will fail. Also if you constantly replace the datasource of the grid, only the last one is shown, all other fetches are in vain.

So this looks like you're trying to fill the data in the grid in two phases, first you fetch the initial set of data and the you try fetch prices for the rows previously fetched.

If this is the case, normally these are not done as separate operations. Instead the data is typically fetched in one call and a join is used to correctly fetch data from two tables at the same time. Have a look at Visual Representation of SQL Joins[^]
 
Share this answer
 
First remove out connection.close out of loop
Try this construct in loop

INSERT INTO table1 / curser SELECT [Price] from [Purchase Record] where [Product Name] = '" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'"

unless fields of table1 match with selected records this will not work.
This Construct will give desired result in loop with all required data.
Only Logic is posted not exact command hope this may help you.
 
Share this answer
 
v2
Comments
Philippe Mori 3-Jan-17 9:21am    
Instead of posting 2 partial answers, you should have post one good 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