none
join problem from linq to sql RRS feed

  • Question

  • what is the error with it.

     VTSDataContext db = new VTSDataContext();



    var saleObj = from s in db.Sales



    join sd in db.SaleDetails on s.Reciept_Number equals sd.Reciept_Number



    join pr in db.Products on sd.product_ID equals pr.product_ID



    where s.Reciept_Number == int.Parse(ddl_RNO_Edit.SelectedItem.Text)



    orderby s.Reciept_Number ascending



    select new



    {

    s.Sale_Date,

    s.CustomerID,

    s.ManagerID,

    s.SalesPersonID,

    sd.product_Quantity,

    pr.product_Name



    };

    foreach (var item in saleObj)

    {

    txt_SDate_Edit.Text = item.Sale_Date.ToString();

    ddl_Customer_Edit.Text = item.CustomerID.ToString();

    txt_SQty_Edit.Text = item.product_Quantity.ToString();

    ddl_ProductName_Edit.Text = item.product_Name.ToString();

    ddl_Manager_Edit.Text = item.ManagerID.ToString();

    ddl_SalesPerson_Edit.Text = item.SalesPersonID.ToString();

    }

    tables are 3:

    CREATE TABLE [dbo].[Sales](
        [Reciept_Number] [int] NOT NULL,
        [Sale_Date] [datetime] NOT NULL,
        [CustomerID] [int] NULL,
        [ManagerID] [int] NULL,
        [SalesPersonID] [int] NULL,
     CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
    (
        [Reciept_Number] ASC
    )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]

    ==========================================================

    CREATE TABLE [dbo].[SaleDetails](
        [Reciept_Number] [int] NOT NULL,
        [SNo] [int] NOT NULL,
        [product_ID] [int] NOT NULL,
        [product_Quantity] [int] NOT NULL,
     CONSTRAINT [PK_SaleDetails] PRIMARY KEY CLUSTERED
    (
        [Reciept_Number] ASC,
        [SNo] ASC
    )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]

    ===============================================================
    CREATE TABLE [dbo].[Products](
        [product_ID] [int] NOT NULL,
        [product_Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
        [product_Category_ID] [int] NOT NULL CONSTRAINT [DF_Table_1_CategoryID]  DEFAULT ((0)),
        [product_Price] [int] NOT NULL,
        [product_Image] [image] NULL,
        [product_Description] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
    (
        [product_ID] ASC
    )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]



    when i try to debug then it throw an exception iin this line:

       ddl_ProductName_Edit.Text = item.product_Name.ToString();



    exception is this:

    System.ArgumentOutOfRangeException was unhandled by user code
      Message="'ddl_ProductName_Edit' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"
      Source="System.Web"
      ParamName="value"
      StackTrace:
           at System.Web.UI.WebControls.ListControl.set_SelectedValue(String value)
           at System.Web.UI.WebControls.ListControl.set_Text(String value)
           at SalesPerson_AddSale.ddl_RNO_Edit_SelectedIndexChanged(Object sender, EventArgs e) in e:\Lectures\sullAll\lectures\finalProject\Final Project Application\VehicleTrackingSystem\SalesPerson\AddSale.aspx.cs:line 218
           at System.EventHandler.Invoke(Object sender, EventArgs e)
           at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
           at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent()
           at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
           at System.Web.UI.Page.RaiseChangedEvents()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException:




    and please convert this to lamda expression

    maifs
    Thursday, December 31, 2009 6:25 PM

Answers

  • Your ListControl - ddl_ProductName_Edit - requires you select an item in the list of items.

    The text you are setting it to does not existing within that list of items. 

    While this is not a LINQ to SQL issue, this could be either:

    1. You forgot to populate the list items for ddl_ProductName_Edit (check .Items)
    2. You are getting back a value you did not expect from the query (check item.product_Name.ToString()) in the query
    3. You meant to use a control that didn't require the item be present for ddl_ProductName_Edit

    [)amien
    • Marked as answer by Mehmood Ahmed Saturday, January 2, 2010 1:00 PM
    Thursday, December 31, 2009 11:19 PM
    Moderator

All replies

  • Your ListControl - ddl_ProductName_Edit - requires you select an item in the list of items.

    The text you are setting it to does not existing within that list of items. 

    While this is not a LINQ to SQL issue, this could be either:

    1. You forgot to populate the list items for ddl_ProductName_Edit (check .Items)
    2. You are getting back a value you did not expect from the query (check item.product_Name.ToString()) in the query
    3. You meant to use a control that didn't require the item be present for ddl_ProductName_Edit

    [)amien
    • Marked as answer by Mehmood Ahmed Saturday, January 2, 2010 1:00 PM
    Thursday, December 31, 2009 11:19 PM
    Moderator
  • can i make that query which take data from database which exits in both table (Sale and SaleDetail) , other data ll not display.

    and if it is possible then plz convert this join expression to lamda expression.

    maifs
    Friday, January 1, 2010 2:20 PM
  • thx to all, i have made that query.


    maifs
    • Marked as answer by Mehmood Ahmed Saturday, January 2, 2010 1:00 PM
    • Unmarked as answer by Mehmood Ahmed Saturday, January 2, 2010 1:00 PM
    Saturday, January 2, 2010 12:59 PM