Wednesday, 13 March 2024

convert list issues

 Severity Code Description Project File Line Suppression State

Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Enviri.SharedLibrary.Models.TblVendor>' to 'System.Collections.Generic.List<Enviri.SharedLibrary.Models.TblVendor>'. An explicit conversion exists (are you missing a cast?) Enviri.PO.WebUI C:\MyFiles\Harsco\CORP_POC\Enviri.PO\Enviri.PO.WebUI\Enviri.PO.WebUI\Implementations\VendorRepository.cs 25 Active
ChatGPT
ChatGPT

The error message CS0266 indicates that you are trying to implicitly convert an IEnumerable<T> to a List<T>, but there's no implicit conversion available between these types. You need to perform an explicit conversion to resolve this issue.

Here's how you can fix it:

csharp
public async Task<List<TblVendor>> GetAllVendors() { var vendors = await _context.TblVendors.ToListAsync(); return vendors; }

In this corrected version:

  • The ToListAsync() method is used to asynchronously retrieve all vendors from the database and materialize them into a list.
  • The method now returns a List<TblVendor> instead of an IEnumerable<TblVendor>.

This should resolve the compilation error you're encountering.

User
You
async Task<List<TblVendor>> IVendorRepository.GetAllVendorsAsync() { using (IDbConnection db = _dapperDbConnection.CreateConnection()) { return await db.QueryAsync<List<TblVendor>>("SELECT * FROM [tbl_Vendors]"); } }
ChatGPT
ChatGPT

The issue in your code is that you're trying to use QueryAsync<List<TblVendor>>, which expects a single List<TblVendor> as a result. However, you're selecting all columns from the table, which would return multiple rows, not a single list.

To fix this, you should use QueryAsync<T> instead, where T is the type of each row in your result set (TblVendor in this case). Here's how you can correct your code:

csharp
using Dapper; async Task<List<TblVendor>> IVendorRepository.GetAllVendorsAsync() { using (IDbConnection db = _dapperDbConnection.CreateConnection()) { return (await db.QueryAsync<TblVendor>("SELECT * FROM [tbl_Vendors]")).ToList(); } }

In this corrected version:

  • We use QueryAsync<T> instead of QueryAsync<List<TblVendor>>.
  • QueryAsync<T> expects the type of each row (TblVendor in this case) and will return an IEnumerable<TblVendor>. We then call ToList() to materialize the result into a List<TblVendor>.

This should resolve the issue, and now your method will correctly return a list of TblVendor objects.

get Html based c# model class

 using System;

using System.Reflection;

using System.Text;


public class ModelToHtmlConverter

{

    public static string GenerateFormHtml<T>()

    {

        var sb = new StringBuilder();

        var type = typeof(T);


        sb.Append("<form method=\"post\">");


        foreach (var property in type.GetProperties())

        {

            var label = property.Name;

            var inputType = GetInputType(property.PropertyType);


            sb.Append($"<div class=\"form-group row mb-3\">");

            sb.Append($"<label class=\"col-md-2 col-form-label\">{label}:</label>");


            if (inputType == "select")

            {

                sb.Append($"<select name=\"{label}\">");

                foreach (var option in GetOptions(property.PropertyType))

                {

                    sb.Append($"<option value=\"{option}\">{option}</option>");

                }

                sb.Append($"</select>");

            }

            else

            {

                sb.Append($"<input type=\"{inputType}\" name=\"{label}\" />");

            }


            sb.Append($"</div>");

        }


        sb.Append("<button type=\"submit\">Submit</button>");

        sb.Append("</form>");


        return sb.ToString();

    }


    private static string GetInputType(Type type)

    {

        if (type == typeof(string))

            return "text";

        else if (type == typeof(int) || type == typeof(double) || type == typeof(float))

            return "number";

        else if (type == typeof(bool))

            return "checkbox";

        else if (type == typeof(DateTime))

            return "date";

        else if (type.IsEnum)

            return "select";

        else

            return "text";

    }


    private static string[] GetOptions(Type type)

    {

        if (type.IsEnum)

            return Enum.GetNames(type);

        else

            return new string[0];

    }

}


// Example usage:

public enum Gender

{

    Male,

    Female,

    Other

}


public class MyModel

{

    public string Name { get; set; } = null!;


    public string Number { get; set; } = null!;


    public string? Address1 { get; set; }


    public string? Address2 { get; set; }


    public string? Address3 { get; set; }


    public string? Address4 { get; set; }


    public string? City { get; set; }


    public string? State { get; set; }


    public string? Zip { get; set; }


    public PaymentTermsId PaymentTermsId { get; set; }


    public bool IsDeleted { get; set; }


    public string CreatedBy { get; set; } = null!;


    public DateTime CreatedOn { get; set; }


    public string ModifiedBy { get; set; } = null!;


    public DateTime? ModifiedOn { get; set; }

}

public enum PaymentTermsId

{

    Male,

    Female,

    Other

}


class Program

{

    static void Main(string[] args)

    {

        string formHtml = ModelToHtmlConverter.GenerateFormHtml<MyModel>();

        Console.WriteLine(formHtml);

    }

}


Transaction in sql

to know pending transaction use below querys 

SELECT * FROM sys.sysprocesses WHERE open_tran = 1


SELECT * FROM sys.sysprocesses WHERE open_tran <> 0

Exec sp_Who2 79

exec sp_lock 79

KILL 79

Friday, 8 March 2024

transient

 https://medium.com/@dotnetcode/difference-between-transient-scoped-and-singleton-type-of-dependency-injection-in-net-core-ebab9c74295b


https://freefrontend.com/css-spinners/


Thursday, 7 March 2024

remove duplicates

 Begin Tran DPID;

with EmCTE As(

select *, Row_number() over(Partition By Approver_Level order by Approver_Level ) as RowNumber

from [dbo].[tbl_WorkFlowProgress]

where AFINumber ='2024MSVTR00160' 

)

select * from EmCTE

INC2244251


DELETE from EmCTE where RowNumber >1

Monday, 4 March 2024

external tools

 add Efcore as external tool in vs2022 here is the link how to use it for scafold the db.

Link1  Link2

Link3

Best ui design