message error CS1525

sblb 1,166 Reputation points
2022-08-03T16:10:18.813+00:00

Hi,

I would like to put a specific value in table when I click on add bouton

class model

public partial class Developer  
{  
        public int Id { get; set; }  
        public string ECR { get; set; }  
        public DateTime DateCrea { get; set; }  
       
 }  

razor page
<div class="col-md-6">
<RadzenTextBox style="width: 100%;" Name="Numéro ECR" @bind-Value=IncrementECR() >
</RadzenTextBox>
<ValidationMessage For="@(() => dev.ECR)" />
</div>

@code {  
    [Parameter] public Developer dev { get; set; }  
  
    private readonly int _currentYear = DateTime.Today.Year;      
  
    public void IncrementECR()  
    {  
        int datecrea = dev.DateCrea.Year;  
  
        if(datecrea == _currentYear)  
        {  
           @dev.ECR = 001 + "/" + _currentYear;                        
             
        }     
    }  
}  

I received the message that I don't understand : the invalid '=' expression term
because I want to assign a value to @dev.ECR and not compare with '=='

FYI, I used the several source to create my application, I list below the different links
blazor-crud-with-entity-framework-core
blazor-api-handling
dashboard

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,395 questions
0 comments No comments
{count} votes

9 answers

Sort by: Newest
  1. Bruce (SqlWork.com) 56,531 Reputation points
    2022-08-11T18:20:45.63+00:00

    you do not seem to think about your problems at all. you wanted the counter to start a 1 for each year. this means you need a separate counter for each year. but you do pass the year to the sp. also you never update the counter in the database, so the same value is always return. you also must think about race conditions (two calls at the same time).

    as simple table:

    create table yearcounter   
    (   
        year int not null unique,   
        counter int not null   
    )   
    

    as simple proc to return the current counter

    create procedure getcount    
       @year int   
    as   
       
    merge yearcounter as target   
    using (select @year as year) as source   
     on source.year = target.year   
    when not matched by target then   
        insert (year, counter)    
        values (source.year, 1)   
    when matched then update    
     set target.counter = target.counter + 1   
    output inserted.*;   
       
    

    sample call:

    exec getcount 2022   
       
    

    year counter
    2022 1

    0 comments No comments

  2. AgaveJoe 26,136 Reputation points
    2022-08-11T17:41:11.623+00:00

    According to this thread and you many others, the main problem is you have no programming or problem solving skills. Giving you code will not help with your understanding. I think a better way to help you is to show an example of how to test code. The follow test sample uses a temp table to hold test data. You can add any test data you like. The script uses the temp table to exercise the logic. This is called unit testing.

    The first test increments the Id.

    If OBJECT_ID('tempdb..#Developer') IS NOT NULL  
     DROP TABLE #Developer  
      
    CREATE TABLE #Developer (  
     DeveloperId INT IDENTITY(10, 1) PRIMARY KEY,  
     Id INT NOT NULL,  
     [Year] INT NOT NULL,  
     CONSTRAINT uniqueId unique (Id, [Year])  
    )  
      
    --Test data  
    INSERT INTO #Developer (Id, [Year])  
    VALUES (1, 2022),(2, 2022),(3, 2022)  
      
    --View data  
    SELECT * FROM #Developer  
    ORDER BY [Year], Id  
      
    --INSERT logic  
    DECLARE @Id INT;  
    DECLARE @year INT;  
      
    SET @year =  YEAR(GETDATE());  
      
    IF EXISTS (SELECT (1) FROM #Developer WHERE [Year] = @year)  
     SELECT @id = MAX(Id) + 1 FROM #Developer;  
    ELSE  
     SET @id = 1;  
      
    INSERT INTO #Developer (Id, [Year])  
    VALUES (@id, @year)  
      
      
    --View data  
    SELECT * FROM #Developer  
    ORDER BY [Year], Id  
    

    Simply change the data inserted into the #Developer table to test how the code handles a new year.

    --Test data  
    INSERT INTO #Developer (Id, [Year])  
    VALUES (1, 2021),(2, 2021),(3, 2021)  
    

    This is the simplest approach I can think of . Keep in mind that I would use the Id and Year as the primary key but I think a composite key is beyond your capabilities.

    Lastly, the PRINT command simply writes to the Message tab which is handy if you need to see a run-time variable. PRINT is helpful for debugging.

    Anyway, happy programming and learn unit testing.


  3. sblb 1,166 Reputation points
    2022-08-11T09:36:21.363+00:00

    to simplify I started by adding only an increment w/o the condition of year

    ALTER PROCEDURE TestIncrement @ECR int  
    AS  
    BEGIN  
    DECLARE @id int  
    

    SET @id =0;
    SELECT * FROM Developers Where Id =@id
    SELECT @id=MAX(@id)+1
    PRINT @id
    SELECT * FROM Developers WHERE ECR = @ECR Computers Inc
    UPDATE Developers
    SET @ECR Computers Inc = @id
    WHERE ECR = @ECR Computers Inc
    END

    I don't understand print @id return nothing and any value is update in column ECR from Developers.


  4. sblb 1,166 Reputation points
    2022-08-07T12:25:27.72+00:00

    Is there anyway you can share the code you've tried?

    I tried to do the stored procedure as below w/o any success :

    CREATE OR ALTER PROCEDURE IncrementNumber(@Id int, @prefix nvarchar(1), @currentYear DateTime)  
    AS   
     
    BEGIN  
    SELECT*FROM Developers WHERE @Id = MAX([Id]);  
    SELECT @currentYear = YEAR(GETDATE());  
    SET @Id = @Id + 1;  
    SET @prefix = '/';  
    SELECT*FROM Developers WHERE [ECR] = CONCAT( @Id, @prefix, @currentYear )   
      
    END  
    

  5. sblb 1,166 Reputation points
    2022-08-05T20:25:10.177+00:00

    why you would put the number in a text input.

    I want to put a specific value which takes a form "001/22" which will be a string value.

    The code to add number increment

           private readonly int _currentYear = DateTime.Today.Year;      
           int counter = 0;  
          private string IncrementECR()  
          {              
                 dev.ECR = counter.ToString("00") + "/" + _currentYear.ToString();  
               counter++;  
    
                return dev.ECR ;  
                
          }  
    

    But there are always the message cs1525