none
DataBinding - Please help me!! i already posted this and nobody answered RRS feed

  • Question

  • hi
    i have a windows application.
    i have a user interface which present data from database, create data from user input that will be added to databse through the application etc'.
    i have 2 question:
    1) i know that in ADO.net i can use dataset and define to the dataset insert, update and delete statement. i can display values with the dataset on grid, i can allow user edit the values and they will be updated on database with no effort by my side. is there a way to do something like that with linq? i know that i can bind table to grid, for example, as the data source but i couldnt succedd binding 2 tables with relations (one has a foreign key from the other) on grid. is there a way to do this? how can i use tables with relation and display the relation?
    in addition i tried binding one table to grid, edit values and than use submit changes function but the data in database is not being updated. is there a way doing that? again, its simple in ado.net but i couldnt find it in linq so i do all of the work by myself
    2) i have a stored procedure which retrieve data from some tables. i display it on grid. user do actions on that and create an entity which i need to insert to database. what is the correct way to work in such case? i am creating my own object of type that is very much like the dbml table (or the database table) that i want to insert the data into. after ceating the object and populating it i transfer the object to the data context layer, create object of the database table type, populate it with the data from the object and insert to database - this is a lot of work. i work like that because it seems to me incorrect to work with tables type of the database in a higher level than the data context layer. is this conccept correct? what is the best way to work in the described example?

    Thursday, November 12, 2009 9:35 AM

Answers

  • Q)  i know that in ADO.net i can use dataset and define to the dataset insert, update and delete statement. i can display values with the dataset on grid, i can allow user edit the values and they will be updated on database with no effort by my side. is there a way to do something like that with linq?

    Ans)

        Yes you can perform the same tasks using LINQ

    Ensure that Context object need to be created for the Database before it will get  used to create the Datasource which is bound with Grid view

    Create a LinqDatasource and bind with the Grid , also via the configure grid task window , user will be given option to enable the actions such as edit, select, delete and paging etc..


    Q ) in addition i tried binding one table to grid, edit values and than use submit changes function but the data in database is not being updated. is there a way doing that? again, its simple in ado.net but i couldnt find it in linq so i do all of the work by myself

    Ans)

    have a look at below code

     

    AdventureWorksDataContext db = new AdventureWorksDataContext();

     

    var Dept = db.Departments.First(D => D.DepartmentID == 1);

    Dept.Name =

    "Tool Design Test";

    db.SubmitChanges(

    );

    This code updates the department name of the department of which department ID is 1 .

    Hope it helps you in some degree

    Thanks
    Jbasingh




    Thanks
    Jebasingh Luccas



    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 2:34 AM
    Saturday, November 14, 2009 5:52 PM
  • check this link for which might be solution for the item # 1

    http://www.asp.net/LEARN/linq-videos/video-247.aspx


    item # 2

    The logic i given above worked for me . have you refreshed the Gridview once you executed the updattion logic code ?

    have you got any error when the code gets executed?, if not then  you need to implement conflict resolution before actually performing the submit changes








    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 2:34 AM
    Monday, November 16, 2009 1:06 PM

All replies

  • Q)  i know that in ADO.net i can use dataset and define to the dataset insert, update and delete statement. i can display values with the dataset on grid, i can allow user edit the values and they will be updated on database with no effort by my side. is there a way to do something like that with linq?

    Ans)

        Yes you can perform the same tasks using LINQ

    Ensure that Context object need to be created for the Database before it will get  used to create the Datasource which is bound with Grid view

    Create a LinqDatasource and bind with the Grid , also via the configure grid task window , user will be given option to enable the actions such as edit, select, delete and paging etc..


    Q ) in addition i tried binding one table to grid, edit values and than use submit changes function but the data in database is not being updated. is there a way doing that? again, its simple in ado.net but i couldnt find it in linq so i do all of the work by myself

    Ans)

    have a look at below code

     

    AdventureWorksDataContext db = new AdventureWorksDataContext();

     

    var Dept = db.Departments.First(D => D.DepartmentID == 1);

    Dept.Name =

    "Tool Design Test";

    db.SubmitChanges(

    );

    This code updates the department name of the department of which department ID is 1 .

    Hope it helps you in some degree

    Thanks
    Jbasingh




    Thanks
    Jebasingh Luccas



    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 2:34 AM
    Saturday, November 14, 2009 5:52 PM
  • thanks for the answer but something is still not clear for me:
    1) i know to bind table to grid. but when user update values of the GRID - is there a way to automaticlly update the values in the database - like    ado.net allow?
    2) i know that in the datacontext layer i can update values of the database - i asked another think - how can user update values on grid (the grid is binded to datacontext object) and i the values will be updated on DB - when i tried something like you wrote with the submit changes after user updated values in the grid it doesnt work
    3) how can i bind 2 tables with foreign key (it should be displayed in 2 bands on grid)
    please help me i am stuck and dont find material on that on google :(
    Monday, November 16, 2009 12:33 PM
  • check this link for which might be solution for the item # 1

    http://www.asp.net/LEARN/linq-videos/video-247.aspx


    item # 2

    The logic i given above worked for me . have you refreshed the Gridview once you executed the updattion logic code ?

    have you got any error when the code gets executed?, if not then  you need to implement conflict resolution before actually performing the submit changes








    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 2:34 AM
    Monday, November 16, 2009 1:06 PM
  •  


    AdventureWorksDataContext
    db = new AdventureWorksDataContext();

     

    var q = from dept in db.Departments

     

    from empdept in dept.EmployeeDepartmentHistories

     

    select new {

    dept.DepartmentID, //source : depratment

    dept.Name, // source : depratment

    dept.GroupName, // source : depratment

    dept.ModifiedDate, // source : depratment

    empdept.EmployeeID,  // source : EmployeeDepartmentHistories
    };

    GridView1.DataSource = q;

    GridView1.DataBind();


    In the above sample code , look at the query part which retrieves data from both department and EmployeeDepartmentHistories  tables whic are mapped with foriegn key . finally the resultset will be bound to grid..

    Hope it answers you third question



    Monday, November 16, 2009 3:11 PM
  • Hi Michal,

    Are you workin with ASP.NET project or winform project?
    There are something different while dealing with them.

    Best Regards
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, November 17, 2009 8:32 AM