DataGridview with linq-to-sql - linq

I have a datagridview which is used for listing all the students information .I am using linq to sql as a datasource.My problem arises when I edit a row or add a new row directly in a gridview.I mean there are no any forms for editing or adding student information.User should type on the particular columns to edit or to add a new row.
I found the soultions which uses SqlDataAdapter .
TableAdapter.Update(e.Row); which updates the database.
I wonder how can I achieve the same functionality using linq to sql.

Call the SubmitChanges() method of the DataContext, which you used to fill the DataGridView.
MSDN reference:
http://msdn.microsoft.com/en-us/library/bb292162.aspx

Related

Oracle Apex 19.1 (how to add, edit, delete data through a form)

I'm new to oracle apex so this might be simple.
I have an application that i'm currently building. I have an 'employee' table in the SQL workshop. When i attempt to create a form linked to the table there is no option to edit, delete, or add entries into the table ,once the form is completed?
This is essentially what i need help with. I need to be able to manipulate the 'employee' table through the form created rather than through anything within the sql workshop. Just for context i am not the workspace admin, however i am a contributor.
I would appreciate if anyone could provide me with a quick step by step guide into creating this desired form accurately.
I'd suggest you to create (using the Wizard, of course) Report with Form on Table. It will create
interactive report you'd use to view data stored in that table
form which will be used to insert/update/delete data
the same form will be called when you push
the "Create" button in order to create new rows, or
icon at the beginning of every line in the report in order to update/delete rows
This combination (report + form) works nicely for ages, so ... try it. I hope you'll find it useful.

LINQ update statement using generic table and column names

OK, I've seen a lot of posts on creating generic LINQ statements, but these are usually based on select queries. What I need is a LINQ UPDATE statement that takes a generic parameter for the table to update and a generic parameter for the column to update.
The data model I'm working with has dozens of tables, and each table has potentially dozens of columns. What I'm really driving toward is a single Update statement that allows me to tell it at runtime which table and which column to update.
Dealing with data typing of these dynamically supplied fields will also be an issue since obviously I can't update a DateTime column with a decimal value for example.
So, can anyone point me at some code that shows a LINQ update process using generic parameters for table and column names.
Any help would be greatly appreciated!
You didn't specify Entity Framework with DbContext but if that is what you are using, this is pretty easy. The following code is written to work within the NerdDinner sample which can be downloaded from http://nerddinner.codeplex.com/
This code locates a dinner based on the id and sets the address property to "New Value"
Dinner dinner = db.Set<Dinner>().Find(id);
var entry = db.Entry(dinner);
entry.Property("Address").CurrentValue = "New Value";
db.SaveChanges();

update query in linq to update the data in multiple tables

I created kendo ui grid.I am able to load the data from the database.For this grid the data is coming from different tables ,I used joins in my linq query to load the data.
Now,I want to update the data using linq query,and update the data in different tables.
Any suggestions..
You have to update individual records from each table and then execute db.SubmitChanges();
In your query the output is an anonymous type, not a table type connected to the dbContext.
If you think in terms of SQL, LinqToSql works very similar.
It´s possible to select a record set with a join, but you cannot update directly on this. You need to break it up and modify entries directly one by one.
A DataContext instance tracks all the instances it loads. When you call SubmitChanges, all of the changes are send to the database in one transaction.

XtraReport and xrTable shows only 1 record

I have a linq to sql generated object MyObject and this contains an EntitySet.
Now i use an XtraReport with a BindingSource.DataSource = MyObject. I have a xrTable in the Detail band (which is the only one i use btw) with four xrTableCells binded binded to properties from MyObject.MyotherObject.Property. It keeps showing me only the first record and not all of them.
Anyone with an idea?
I believe you'll need to implement the ITypedList with your custom object to use it as a datasource. Check out this online documentation article for more info:
http://www.devexpress.com/Help/?document=XtraReports/CustomDocument4046.htm
I have just solved a similar problem. Did you use typed datasets to help you layout the report at design time? If you did then clear the Data Source in design view. If you apply LINQ to SQL Data Source at runtime without clearing the typed dataset Data Source in the design view, you may get only one row showing in the detail band.
i have similar problem and tried above answers, but it was so simple
just set your report DataMember to the name of your sub collection MyObject.MyotherObject
and the table will load all items in your sub collection.
XtraReport report = new XtraReport(){DataSource = MyObject,DataMember="MyotherObject" };

How do I edit a Telerik RadGrid when using multiple tables as a data source

I have asked this question on Telerik forums with no reply so I'm turing to you.
I have seen this topic come up before but without an answer so I thought I'd define the problem as simply as possible.
I have a LINQ command creating a query:
var gridData = from au in dc.aspnet_Users
join aur in dc.aspnet_UsersInRoles
on au.UserId equals aur.UserId
join ar in dc.aspnet_Roles
on aur.RoleId equals ar.RoleId
join am in dc.aspnet_Memberships
on au.UserId equals am.UserId
select new
{
au.UserName,
ar.RoleName,
am.Email,
am.IsApproved,
am.IsLockedOut
};
THE IMPORTANT POINT IS THAT THIS USES MORE THAN 1 TABLE.
Note: This is accessing the Roles and Users tables using the standard ASP.NET Membership framework.
Then I have a simple RadGrid:
<MasterTableView EditMode="InPlace">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
Finally I bind the data to the grid in Page_Load
rgUsers.AutoGenerateColumns = true;
rgUsers.DataSource = gridData.ToList();
rgUsers.DataBind();
rgUsers.EditIndexes.Add(1);
rgUsers.DataBind();
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = gridData.ToList();
GridView1.DataBind();
GridView1.EditIndex=1;
GridView1.DataBind();
This will display the data from the 4 tables correctly.
I included a GridView to compare functionality.
I also put the grid into Edit mode programatically to make sure that this was not just a problem with the Edit button.
However, when in Edit mode the RadGrid fields are not editable.
I have tried the following:
1) 1) I can use a LinqDataSource that allows Edit/Update BUT only if there is only 1 table in the LINQ query.
2) 2) I can use a ObjectDataSource onto a dataset but once again this only works if the the dataset adaptor is accessing a single table.
I have looked at the variouse suggested demos and example code but they have all ignored the multiple table/Join problem:
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
I am not wedded to LINQ. I am quite happy to adapt in order to get a RadGrid that can support Update and Edit to multiple tables.
What should I do to achieve a RadGrid that displays records composed from multiple tables with Update and Edit available?
Thanks for any help
Richard
Have you considered using ADO.net's dataview class in conjunction with a dataset class? You should be able to pull you data using the system.data.sqlclient class with a stored proc if that is the type of database you're using. You can preserve your database's relationships in the dataset class and the dataview class will allow you to view the data as if it were a single table. Then you should be able to bind the dataview class to the Telerik control.
Why not using NeedDataSource binding as opposed to DataBind() calls? Thus the Telerik grid should manage its editing states without extra coding. If you use joins for different source tables and set you update or insert commands using appropriate T-SQL statements, everything should work fine. Check out this demo for a start:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
Dick
Turns out that the LINQ query returns an anonymous type that is read only.
I replaced it with a SQLDataSource using stored procedures so it works now.
Thanks

Resources