1

I face the problem with datasource of datagridview which uses stored procedure and the error is

The data reader is incompatible with the specified 'DatabaseModel.GetInfo_Result'. A member of the type, 'CategoryExpensesId', does not have a corresponding column in the data reader with the same name`.

The stored procedure that I use:

Create PROCEDURE [dbo].[GetInfo] AS 
Begin 
    SELECT [Period] ,[Cost] ,[Name] ,[Comments] 
    FROM [dbo].[Expenses] as с 
    inner join [dbo].[CategoryExpenses] as cat on
    с.CategoryExpensesId=cat.Id 
End

[CategoryExpenses] (
        [Id]           INT           IDENTITY (1, 1) NOT NULL,
        [Name] NVARCHAR (50) NOT NULL,`--I want to use this column in stored procedure with inner join so that datagridview shows name of category instead of Id`
        PRIMARY KEY CLUSTERED ([Id] ASC)

[dbo].[Expenses] (
    [Id]                INT            IDENTITY (1, 1) NOT NULL,
    [Period]            DATETIME2 (7)  NOT NULL,
    [Cost]         DECIMAL (18)   NOT NULL,
    [Comments]       NVARCHAR (MAX) NULL,
    [CategoryExpensesId] INT            NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Expenses_ToTable] FOREIGN KEY ([CategoryExpensesId]) REFERENCES [dbo].[CategoryExpenses] ([Id])
);

C# code

  void PopulateComboboxWithDataGrid()
        {
            btnDelete.Enabled = false;
            dataGridView1.DataSource = db.GetInfo().ToList();

            cbxCategory.DataSource = db.CategoryExpenses.ToList();
            cbxCategory.ValueMember = "Id";
            cbxCategory.DisplayMember = "Name";
            cbxCategory.SelectedItem = null;
            cbxCategory.SelectedText = "--ChooseCategory--";
        }
Gonzalo Lorieto
  • 635
  • 6
  • 24
mir 433
  • 15
  • 4
  • @IsaacAbramowitz, Hi Isaac I have edited my question I think now it is possible for you to help:) – mir 433 Jul 31 '18 at 18:44
  • Can you post `Expenses` structure? – Gonzalo Lorieto Jul 31 '18 at 18:46
  • Are you letting the DataGridView `AutogenerateColumns` or did you define them in the designer? If defined in the designer, what are the column names? – HardCode Jul 31 '18 at 19:05
  • Post it on the question through edit – Gonzalo Lorieto Aug 01 '18 at 03:14
  • @GonzaloLorieto, Done Gonzalo I have edited – mir 433 Aug 01 '18 at 03:20
  • I'm trying to parse the error - been a while since I've worked w/ datareader. " A member of the type, 'CategoryExpensesId'" is saying, in your code, DatabaseModel.GetInfo_Result has a member (property/field) called 'CategoryExpensesId'. Then, "does not have a corresponding column in the data reader" - the DataReader does not contain this column (as other person asked, are these autogenerated, or defined?). Last, based on the error, `DatabaseModel.GetInfo_Result` contains a field `CategoryExpensesId`, but the stored proc doesn't return that. Is that a mistake? – p e p Aug 01 '18 at 03:28
  • One more thing, add your `C#` code – Gonzalo Lorieto Aug 01 '18 at 03:40
  • @GonzaloLorieto, Done – mir 433 Aug 01 '18 at 03:49
  • @pep Hi, columns are autogenerated – mir 433 Aug 01 '18 at 03:53
  • Cool. I'm just going with my gut feeling - the error is likely caused due to `DatabaseModel.GetInfo_Result` containing properties that you aren't returning from the stored proc, which returns columns `[Period][Cost][Name[Comments]`. I bet that if either a) the stored proc returns all columns in `GetInfo_Result`, or if `GetInfo_Result` is updated to remove the cols not returned by the stored proc, this would work. But I'm not posting this as an answer because I'm not 100%. – p e p Aug 01 '18 at 03:55
  • Check this https://stackoverflow.com/questions/32140774/getting-data-from-stored-procedure-with-entity-framework. Have you done this that way? – Gonzalo Lorieto Aug 01 '18 at 12:42

0 Answers0