How to refresh a datatable??

A

Alex

Hello!
I have a small prob, so pls help me to solve it.
I have 2 tables
Products(ProductID, ProductName, Price)
Stores(StoreID, StoreName)
now I must design a table for Income-Product: IncomeProducts(id, Date ,
ProductID, Quantity, Price, Total, StoreID).
But I want to display the table IncomeProducts in the DataGrid like that :
| ProductName | Price | Quantity | Total | StoreName
To do it: I use VIEW to make a visual Table (IncomeProductView):
SELECT dbo.IncomeProducts.[Date],
dbo.Products.ProductName,dbo.IncomeProducts.Price,
dbo.IncomeProducts.Quantity, dbo.IncomeProducts.Total,
dbo.Stores.StoreName
FROM dbo.IncomeProducts INNER JOIN
dbo.Products ON dbo.IncomeProducts.ProductID =
dbo.Products.ProductID INNER JOIN
dbo.Stores ON dbo.IncomeProducts.StoreID =
dbo.Stores.StoreID




And now I display this View on the DataGrid
....
SqlDataAdapter sda..
DataSet ds
sda.Fill(ds, "IncomeProductView");
DataGrid dg;
dg.DataSource = ds.Tables["IncomeProductView"];
.....

1. Want to ask you If I do correctly????
2. Now Everytime when i want to add a new 'row' to the table IncomeProducts,
I want it to be displayed in the DataGrid ( to do that, the View must
refresh to get the new inserted row). How can I do it???? or I must Clear
the ds and "Download" again??? It's very bad......


Thanks
 
W

W.G. Ryan eMVP

1. Want to ask you If I do correctly????
From the looks of everything, it looks ok
2. Now Everytime when i want to add a new 'row' to the table
IncomeProducts,

From the looks of how this is set up, you have two options. One is that you
can store the Datatable in session or viewstate, and each times someone
makes an addition, add a datarow to the datatable with the new information.
When they are done with all of their editing, you can pass the datatable to
an adapter configured to handle the update. Or, each time you can call
update. In most cases you'll probably want to opt for the latter. Assuming
that your update is successful, the database and the local datatable should
look the same with the exception of new rows added/deleted/modified by other
users. If this isn't a problem, then you don't need to requery the database
b/c the datatable and the database will match as soon as you fire the
update. If you absolutely needed to have the data match and knew that you
didn't want to overwrite any data that was changed by someone else, the
safest way to do it is check for new data with a new select and repopulate.
There are a few ways to go about this but this is what you are trying to
avoid right?

Anway, by just submitting your updates your table and db will match as long
as no exceptions are raised so I think you're good to go.

If I misunderstood what you were asking though or didn't answer your
question to your satisfaction, please let me know.

Cheers,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Alex said:
Hello!
I have a small prob, so pls help me to solve it.
I have 2 tables
Products(ProductID, ProductName, Price)
Stores(StoreID, StoreName)
now I must design a table for Income-Product: IncomeProducts(id, Date ,
ProductID, Quantity, Price, Total, StoreID).
But I want to display the table IncomeProducts in the DataGrid like that :
| ProductName | Price | Quantity | Total | StoreName
To do it: I use VIEW to make a visual Table (IncomeProductView):
SELECT dbo.IncomeProducts.[Date],
dbo.Products.ProductName,dbo.IncomeProducts.Price,
dbo.IncomeProducts.Quantity, dbo.IncomeProducts.Total,
dbo.Stores.StoreName
FROM dbo.IncomeProducts INNER JOIN
dbo.Products ON dbo.IncomeProducts.ProductID =
dbo.Products.ProductID INNER JOIN
dbo.Stores ON dbo.IncomeProducts.StoreID =
dbo.Stores.StoreID




And now I display this View on the DataGrid
...
SqlDataAdapter sda..
DataSet ds
sda.Fill(ds, "IncomeProductView");
DataGrid dg;
dg.DataSource = ds.Tables["IncomeProductView"];
....

1. Want to ask you If I do correctly????
 
A

Alex

Hello Bill!
Thank you so much for your help and enthusiasm.
I think that all you said is very correct in the case the DataTable points
to a real Table. But in my case, the DataTable, in fact, is a VIEW (so as I
understand, It's a RUNTIME QUERY or VIRTUAL Table), it means that I can not
Insert/Delete/Modify any data in the VIEW directly, instead of it, i can
only Insert/Delete/Modify the data in the table IncomeProducts. In order to
display all the updated data in the DataGrid (DataGrid is used to display
the VIEW), I must execute the Query again (refresh VIEW again--->load all
data again), right???
Regards!




W.G. Ryan eMVP said:
1. Want to ask you If I do correctly????
From the looks of everything, it looks ok
2. Now Everytime when i want to add a new 'row' to the table
IncomeProducts,

From the looks of how this is set up, you have two options. One is that you
can store the Datatable in session or viewstate, and each times someone
makes an addition, add a datarow to the datatable with the new information.
When they are done with all of their editing, you can pass the datatable to
an adapter configured to handle the update. Or, each time you can call
update. In most cases you'll probably want to opt for the latter. Assuming
that your update is successful, the database and the local datatable should
look the same with the exception of new rows added/deleted/modified by other
users. If this isn't a problem, then you don't need to requery the database
b/c the datatable and the database will match as soon as you fire the
update. If you absolutely needed to have the data match and knew that you
didn't want to overwrite any data that was changed by someone else, the
safest way to do it is check for new data with a new select and repopulate.
There are a few ways to go about this but this is what you are trying to
avoid right?

Anway, by just submitting your updates your table and db will match as long
as no exceptions are raised so I think you're good to go.

If I misunderstood what you were asking though or didn't answer your
question to your satisfaction, please let me know.

Cheers,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Alex said:
Hello!
I have a small prob, so pls help me to solve it.
I have 2 tables
Products(ProductID, ProductName, Price)
Stores(StoreID, StoreName)
now I must design a table for Income-Product: IncomeProducts(id, Date ,
ProductID, Quantity, Price, Total, StoreID).
But I want to display the table IncomeProducts in the DataGrid like that :
| ProductName | Price | Quantity | Total | StoreName
To do it: I use VIEW to make a visual Table (IncomeProductView):
SELECT dbo.IncomeProducts.[Date],
dbo.Products.ProductName,dbo.IncomeProducts.Price,
dbo.IncomeProducts.Quantity, dbo.IncomeProducts.Total,
dbo.Stores.StoreName
FROM dbo.IncomeProducts INNER JOIN
dbo.Products ON dbo.IncomeProducts.ProductID =
dbo.Products.ProductID INNER JOIN
dbo.Stores ON dbo.IncomeProducts.StoreID =
dbo.Stores.StoreID




And now I display this View on the DataGrid
...
SqlDataAdapter sda..
DataSet ds
sda.Fill(ds, "IncomeProductView");
DataGrid dg;
dg.DataSource = ds.Tables["IncomeProductView"];
....

1. Want to ask you If I do correctly????
I want it to be displayed in the DataGrid ( to do that, the View must
refresh to get the new inserted row). How can I do it???? or I must Clear
the ds and "Download" again??? It's very bad......


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top