locked
Basic LINQ issue... RRS feed

  • Question

  • User2057472149 posted

        Protected Sub LinqDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs)
            Dim myDB As testimonialLINQ
            Dim myTestimonial = From p In myDB.testimonial Select p
            
            GridView1.DataSource = myTestimonial
            GridView1.DataBind()
        End Sub

    I'm doing the most basic LINQ ever, just to start learning LINQ. I've read tutorials, watched videos and gone to MSDN to find an answer. I'm an advanced object oriented programmer and I've been using ASP.net for a few months and I'm quite versed in objects and how they work.

    I dont know what im missing below but my output is empty everytime, i dont get an error or a null, it just returns empty my webpage shows up blank. Can anyone help? The table itself isnt empty. The database has 2 fields...id and testimonial totalling 6 rows.. I have a list view object to display those 6 rows. I set the data source and I bind it...can anyone help?



    testimonialLINQ is my database in my DataClasses.dbml file.

    Protected Sub myLINQ (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs)

            Dim myDB As testimonialLINQ
            Dim myTestimonial = From p In myDB.testimonial Select p

            GridView1.DataSource = myTestimonial
            GridView1.DataBind()

        End Sub


                    <asp:GridView ID="GridView1" runat="server">
                    </asp:GridView>

    Tuesday, July 20, 2010 8:48 PM

Answers

  • User1224194097 posted

    OK,

    check this once pls

    [url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/d7403749f0.gif[/img][/url]

     if (!IsPostBack)
                {
                    gvData.DataSource = (new DataClasses1DataContext()).refOffices.ToList();
                    gvData.DataBind();
                }


    [url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/0f278779ce.gif[/img][/url]

    let me know if you need any help

    VB:

    If (Not IsPostBack) Then
                gvData.DataSource = (New DataClasses1DataContext()).refOffices.ToList()
                gvData.DataBind()
            End If


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 10:53 PM
  • User1224194097 posted

    OK, if everything is good, check if you had the AutoGenerateColumns set to true in the gridview markup.

    make sure you have data also.

    Debug and see what the list is returning, check the count of the list

    If that does not work, post your html markup, we'll try to fix it


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:07 PM
  • User1224194097 posted

    Basically, your gridview should be like this

    <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="True">
        </asp:GridView>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:11 PM
  • User1224194097 posted

    You can define your columns

    But, if you want to just display the data, you do need anything, That's it

    I should have told you earlier to use the AutoGenerateColumns,

    I thought that you had defined the columns.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:44 PM

All replies

  • User1224194097 posted

    I think when you create a DBML Layout, it creates a list of Testimonials for you.

    As you do not have any conditions, you can do this directly I think

    GridView1.DataSource=myDB.testimonials

    GridView1.DataBind()

    Tuesday, July 20, 2010 9:55 PM
  • User2057472149 posted

    Believe it or not, it still doesn't work...I dont know what im doing wrong...I just deleted the DBML file and recreated it. I pasted the XML below just to verify we are using the right information...

    I dont know how much easier I can make this for it to work haha, let me know your thoughts, thanks for your help



    <?xml version="1.0" encoding="utf-8"?>

    <ordesignerObjectsDiagram dslVersion="1.0.0.0" absoluteBounds="0, 0, 11, 8.5" name="DataClasses">

      <DataContextMoniker Name="/DataClassesDataContext" />

      <nestedChildShapes>

        <classShape Id="47886505-233c-45a0-b465-cd607f26f7a4" absoluteBounds="1, 1.25, 2, 1.0961531575520831">

          <DataClassMoniker Name="/DataClassesDataContext/testimonialLINQ" />

          <nestedChildShapes>

            <elementListCompartment Id="f67bbe1e-b7a7-42b8-9c6e-954891321ad0" absoluteBounds="1.0150000000000001, 1.71, 1.9700000000000002, 0.53615315755208326" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />

          </nestedChildShapes>

        </classShape>

      </nestedChildShapes>

    </ordesignerObjectsDiagram>



    Protected Sub myLINQ(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs)

            Dim myDB As testimonialLINQ

            GridView1.DataSource = myDB.testimonial

            GridView1.DataBind()

        End Sub

    Tuesday, July 20, 2010 10:16 PM
  • User626880745 posted

    you should be using

    Dim myDB As New YourDataContext()

    so looks like:

    Dim myDB As New DataClassesDataContext()

    Dim myTestimonial = from ........
    .

    .

    Tuesday, July 20, 2010 10:19 PM
  • User2057472149 posted

    testimonialLINQ is what shows in my DataClasses.dbml file. as I dragged over my table. So as you can see below my syntax is correct right?



    Protected Sub myLINQ (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs)

            Dim myDB As testimonialLINQ
            Dim myTestimonial = From p In myDB.testimonial Select p

            GridView1.DataSource = myTestimonial
            GridView1.DataBind()

        End Sub

    Tuesday, July 20, 2010 10:24 PM
  • User1224194097 posted

    OK,

    Whenever you create DBML Layout by dragging and dropping tables from Server Explorer, It creates a Read Only collection of you Table Records.

    for example, in your case, table is Testimonial, so It creates a ReadOnly collection of Testimonial Records from your Table.

    that Property will be Testimonials.

    bind the GridView to YouDatacontext.Testimonials.ToList();

    which should display your 6 Records.


    Tuesday, July 20, 2010 10:26 PM
  • User1224194097 posted

    It should be like this

    Dim myDB As New YourDataContextName()
            GridView1.DataSource = myDB.Testimonials.ToList()
            GridView1.DataBind()

    Tuesday, July 20, 2010 10:31 PM
  • User626880745 posted

    my syntax is correct right?

    its not the syntax, but the right DataContext

    I believe your DC is DataClassesDataContext, and testimonialLINQ is your mapped entity

    so, again, something like:

    Dim myDB As New DataClassesDataContext()
    Dim myTestimonial = From p In myDB.testimonialLINQ Select p

    GridView1.DataSource = myTestimonial
    GridView1.DataBind()



    here's a sample: 

            Dim d As New NWindOrderDetailsDataContext()
            Dim myOrders = From o In d.Orders Select o
            GridView1.DataSource = myOrders
            GridView1.DataBind()


    @sansan: it will bind to myTestimonial


    Tuesday, July 20, 2010 10:34 PM
  • User2057472149 posted

    Again, both of your solutions compile but produce no output. So I decided to start from scratch. I open the table, the records are there. I see the table that I dragged onto the DBML layout. So I looked for my DataClassName I found it in the XML file <DataContextMoniker Name="/DataClassesDataContext" />

    So I used DataClassesDataContext() and did this....no output

    Dim myDB As New DataClassesDataContext()

    GridView1.DataSource = myDB.Testimonials.ToList()
    GridView1.DataBind()


    THEN I did sansan's solution...

            Dim myDB As New DataClassesDataContext()
            Dim myTestimonial = From p In myDB.testimonialLINQs Select p
            GridView1.DataSource = myTestimonial
            GridView1.DataBind()

            Dim myDB As New DataClassesDataContext()
            Dim myTestimonial = From p In myDB.testimonialLINQs Select p
            GridView1.DataSource = myTestimonial
            GridView1.DataBind()


    No good either...both compile both run and I rebuild the project before I run it. Still no output...weird!!?

    Tuesday, July 20, 2010 10:45 PM
  • User1224194097 posted

    OK,

    check this once pls

    [url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/d7403749f0.gif[/img][/url]

     if (!IsPostBack)
                {
                    gvData.DataSource = (new DataClasses1DataContext()).refOffices.ToList();
                    gvData.DataBind();
                }


    [url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/0f278779ce.gif[/img][/url]

    let me know if you need any help

    VB:

    If (Not IsPostBack) Then
                gvData.DataSource = (New DataClasses1DataContext()).refOffices.ToList()
                gvData.DataBind()
            End If


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 10:53 PM
  • User2057472149 posted

    Yea, I rebuild the project with the code you added. I changed it to fit my code


            If (Not IsPostBack) Then
                GridView1.DataSource = (New DataClassesDataContext()).testimonialLINQs.ToList()
                GridView1.DataBind()
            End If

            If (Not IsPostBack) Then

                GridView1.DataSource = (New DataClassesDataContext()).testimonialLINQs.ToList()

                GridView1.DataBind()

            End If


    It compiles runs but nothing happens...no results still...

    Tuesday, July 20, 2010 11:00 PM
  • User1224194097 posted

    OK, if everything is good, check if you had the AutoGenerateColumns set to true in the gridview markup.

    make sure you have data also.

    Debug and see what the list is returning, check the count of the list

    If that does not work, post your html markup, we'll try to fix it


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:07 PM
  • User1224194097 posted

    Basically, your gridview should be like this

    <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="True">
        </asp:GridView>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:11 PM
  • User2057472149 posted

    Yea thats it...

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True">

        </asp:GridView>


    Do I need to add anything else?

    Tuesday, July 20, 2010 11:41 PM
  • User1224194097 posted

    You can define your columns

    But, if you want to just display the data, you do need anything, That's it

    I should have told you earlier to use the AutoGenerateColumns,

    I thought that you had defined the columns.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 11:44 PM
  • User626880745 posted

    FYI, AutoGenerateColumns is by default true, you don't have to set it on the GridView as such. 

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx

    Type: System.Boolean
    true to automatically create bound fields for each field in the data source; otherwise, false. The default is true.

    Tuesday, July 20, 2010 11:52 PM
  • User2057472149 posted

    Actually I dont care how it displays, datalist, datagrid, dataview...as long as I can see something...any ideas?

    Tuesday, July 20, 2010 11:54 PM
  • User626880745 posted


    Smile ...I'll try to make this my last post on this thread, we shouldn't be taking 16 posts for a basic question. I'd prefer to opt out if that helps. 


    if you do have data (however so you may fetch it, get it...) and it implements IListSource, IEnumerable or IDataSource...it will bind to a GridView. the GridView autogenerates BoundColumns of data for you IF you don't specify any in the markup - in that respect IF you haven't delved into creating TemplateFields and such, the GridView is your best bet. 

    your markup can be as:

    <asp:GridView ID="GridView1" runat="server"/> 

    and that will do. 


    The issue really has been whether you can confirm that you have data. 

    Actually, without doing a New ...you should not even be able to compile and the L2S query would show a squiggly line over myDB.testimonial

    It is important that you have a primary key defined on the table. and that you save your .dbml and compile. 

    For the GridView take a new page if that helps. 

    To see if your GridView is alright by itself do something like this:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    GridView1.DataSource = "abc"
    GridView1.DataBind()
    End Sub


    On the other hand you need to debug (which you aren't :) ...and check the result of your L2S query (it would give you a count with  myTestimonial.Count())

    Wednesday, July 21, 2010 12:19 AM