Showing posts with label datagrid. Show all posts
Showing posts with label datagrid. Show all posts

Friday, March 30, 2012

How to trigger the update command of the SQLDataSource

Hi - I'm using .net2, and have a gridview, populated by a SQL Datasource (Edit, Insert, Delete, Select).

Like we all used to do with the datagrid, I've added text boxes into the footer, and a link button, which I'd like to use to fire the Update command.

How do I get the link button to trigger the update command?

Thanks, Mark

in you link button click event handler call

datasourceName.Update();

Hop this help

Monday, March 26, 2012

How to trace the records that have been related to the child table

The problem of mine is, I have a datagrid, Which displays data from a Employee(parent) table.

Now I want to delete some records based on the user selected checkbox,
only those records which has no related records in the EmployeeProject(child) can be deleted.
I want to know which are all the record that cannot be deleted?

How can I achieve this?

You need to find out the related records on the child tables by following the primary/foreign key relationships. In the future you can use theON DELETE CASCADE command, or similar, when you create a table, so it will delete all child records automatically. If you don't know the table relationships, I suggest you find out as much as possible about the structure you're working with before allowing data deletion.

|||

SELECT Employee.*,CASE WHEN EXISTS(SELECT * FROM EmployeeProject ep WHERE ep.EmployeeID=e.EmployeeID) THEN 0 ELSE 1 END AS Deletable

FROM Employee e

Then in your datagrid, make a button column that has a commandname of "Delete". Convert that column to a template field. Now databind the button's visible in the template field to Deletable. This isn't a perfect solution because changes can take place in the database from the time you generate the page until you get back the request to delete, but it handles the vast majority of cases. Just make sure you handle the case where you are requested to delete a record that either no longer exists (Most code will just silently fail anyhow), or is no longer deletable (Try/catch the attempt to delete, and on failure, re-databind the grid and toss up an error to the user).

|||

Sorry, didn't notice that you wanted checkboxes. Use the same query, just databind the checkbox'ed enabled property to the Deletable field.

Wednesday, March 7, 2012

How to store/display more than one tables/recordset

see the follow code:
can return more than one recordset/table
the datagrid can show more than one table at a time.
1.the problem is how to store more than one table in a dataset at a time.
2.see sql(2),sql(3), the new table 't1' is empty, after exec sql(3) will meet a "System.Data.OleDb.OleDbException",the sql(2) can pass without return recordset, but sql(3) return a null recordset(maybe). What should I do.
thanks.

Public Function m_execsql()
Dim connStr As String
connStr = "User ID=sa;Password=sa;Initial Catalog=Northwind;Provider=""SQLOLEDB.1"";Data Source=MINGLEI"
conn = New System.Data.OleDb.OleDbConnection(connStr)
conn.Open()
Dim sql(3) As String
sql(0) = "select * from products;"
sql(1) = "select * from orders;"
sql(2) = "create table t1;"
sql(3) = "seletc * from t1;"
Dim i As Integer
For i = 0 To sql.GetUpperBound(0) - 1
Try
da = New OleDb.OleDbDataAdapter(sql(i), conn)
Catch e As Exception
MessageBox.Show(e.Message)
End Try
'Try
ds = New DataSet("dsTable")
dt = New DataTable
da.Fill(ds)
'grd.SetDataBinding(ds, i.ToString)
grd.DataSource = dt
'Catch eds As Exception
' MessageBox.Show(eds.Message)
'End T
Next
conn.Close()
conn = Nothing
End Functioniqueen, you can use multiple data adapters to fill one dataset or if your sql statement returned more than one result set.

Here's more from MS KB:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpopulatingdatasetfromdataadapter.asp

You may want post in .Net section too...