Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how would i filter results on my bindingnavigator when using a stored procedure call with linq?

how would i filter results on my bindingnavigator when using a stored procedure call with linq?

Scheduled Pinned Locked Moved C#
databasecsharpsql-serverlinqsysadmin
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    I have a stored procedure that takes and gives me all the results based on authorFirst and authorLast works fine when i execute it in sql server

    USE [Books]
    GO
    /****** Object: StoredProcedure [dbo].[usp_SelectBasedonAuthor] Script Date: 02/08/2010 17:52:59 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /* Name: usp_SelectBasedonAuthor
    Description: Get authorfirst and last from user and do a select to get all the rows
    meeting that criteria
    Author: Tony Stegall
    Modification Log: Change

    Description Date Author
    Created Procedure 2/8/2010 Tony Stegall

    */
    ALTER PROCEDURE [dbo].[usp_SelectBasedonAuthor]
    --Variables
    @AuthorFirst varchar(50),
    @AuthorLast varchar(50)

    AS

    SELECT Book.*, Authors.*
    FROM Book INNER JOIN
    (Authors INNER JOIN BookAuthors ON Authors.ID = BookAuthors.AuthorID)
    ON Book.ID = BookAuthors.BookID
    WHERE AuthorFirst = @AuthorFirst AND AuthorLast = @AuthorLast;

    in c# I have the following but obviously it is not filtering correctly and I'm not understanding why

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
    try
    {
    using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
    {

                    String\[\] splitstring = null;
                    string treeviewtext = treeView1.SelectedNode.Text;
                    splitstring = treeviewtext.Split(',');
                    var filter = db.usp\_SelectBasedonAuthor(authorFirstTextBox.Text, authorLastTextBox.Text);
    
                    bindingNavigator1.BindingSource.DataSource = filter.ToList();
    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
    
            
        }
    

    My objective is to click on the author name in the treeview and simply call the stored procedure based on that. and pass the results back so i can navigate the results using the bindingnavigator

    T 1 Reply Last reply
    0
    • T tonyonlinux

      I have a stored procedure that takes and gives me all the results based on authorFirst and authorLast works fine when i execute it in sql server

      USE [Books]
      GO
      /****** Object: StoredProcedure [dbo].[usp_SelectBasedonAuthor] Script Date: 02/08/2010 17:52:59 ******/
      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO
      /* Name: usp_SelectBasedonAuthor
      Description: Get authorfirst and last from user and do a select to get all the rows
      meeting that criteria
      Author: Tony Stegall
      Modification Log: Change

      Description Date Author
      Created Procedure 2/8/2010 Tony Stegall

      */
      ALTER PROCEDURE [dbo].[usp_SelectBasedonAuthor]
      --Variables
      @AuthorFirst varchar(50),
      @AuthorLast varchar(50)

      AS

      SELECT Book.*, Authors.*
      FROM Book INNER JOIN
      (Authors INNER JOIN BookAuthors ON Authors.ID = BookAuthors.AuthorID)
      ON Book.ID = BookAuthors.BookID
      WHERE AuthorFirst = @AuthorFirst AND AuthorLast = @AuthorLast;

      in c# I have the following but obviously it is not filtering correctly and I'm not understanding why

      private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
      {
      try
      {
      using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
      {

                      String\[\] splitstring = null;
                      string treeviewtext = treeView1.SelectedNode.Text;
                      splitstring = treeviewtext.Split(',');
                      var filter = db.usp\_SelectBasedonAuthor(authorFirstTextBox.Text, authorLastTextBox.Text);
      
                      bindingNavigator1.BindingSource.DataSource = filter.ToList();
      
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
      
              
          }
      

      My objective is to click on the author name in the treeview and simply call the stored procedure based on that. and pass the results back so i can navigate the results using the bindingnavigator

      T Offline
      T Offline
      tonyonlinux
      wrote on last edited by
      #2

      I even tried doing the following but nothing seems to help...

      try
      {

                  using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                  {
                      
                      
                                                                    
                                            
                      String\[\] splitstring = null;
                      string treeviewtext = treeView1.SelectedNode.Text;
                      splitstring = treeviewtext.Split(',');
                      var query = db.Books.SelectMany(o => o.BookAuthors.Where(a => a.Author.AuthorFirst == splitstring\[1\].ToString()));
      
                      bookBindingSource.DataSource = query.ToList();
      
      
                    
                      
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
      

      do i have to format the returned data somehow in order for the datasoure to understand it ?

      T 1 Reply Last reply
      0
      • T tonyonlinux

        I even tried doing the following but nothing seems to help...

        try
        {

                    using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                    {
                        
                        
                                                                      
                                              
                        String\[\] splitstring = null;
                        string treeviewtext = treeView1.SelectedNode.Text;
                        splitstring = treeviewtext.Split(',');
                        var query = db.Books.SelectMany(o => o.BookAuthors.Where(a => a.Author.AuthorFirst == splitstring\[1\].ToString()));
        
                        bookBindingSource.DataSource = query.ToList();
        
        
                      
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
        

        do i have to format the returned data somehow in order for the datasoure to understand it ?

        T Offline
        T Offline
        tonyonlinux
        wrote on last edited by
        #3

        Okay well I got it work sorta but i'm having an issue with the where clause. If i hardcode the value it works fine but when I take and pass it the splitstring[1].tostring() which according to the debugger contains "tony" then i get no results..

        1. String[] splitstring = null;

        2.                 string treeviewtext = treeView1.SelectedNode.Text.Trim();
          
        3.                 splitstring = treeviewtext.Split(',');
          
        4.                 var query = from b in db.Books
          
        5.                             join ba in db.BookAuthors on b.ID equals ba.BookID
          
        6.                             join a in db.Authors on ba.AuthorID equals a.ID
          
        7.                             join bn in db.BookNumbers on ba.BookID equals bn.BookID
          
        8.                             where a.AuthorFirst ==splitstring\[1\].tostring()
          
        9.                             select b;
          

        at line 9 if i make that where.a.AuthorFirst == "tony" all is fine however if i leave it like it is then the query ends up being null. if i set splitstring[1] = "tony" in here specifically then it works. does spaces matter or am I overlooking something simple here ?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups