Posts

How to Convert List of objects to datatable in c#, ado.net

public static DataTable CreateDataTable<T>(IEnumerable<T> list) {     Type type = typeof(T);     var properties = type.GetProperties();           DataTable dataTable = new DataTable();     foreach (PropertyInfo info in properties)     {         dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));     }     foreach (T entity in list)     {         object[] values = new object[properties.Length];         for (int i = 0; i < properties.Length; i++)         {             values[i] = properties[i].GetValue(entity);         }         dataTable.Rows.Add(values);     }     return dataTable; }

how to extract or get data from multiple rows in single row by eliminating null values in sql server

Image
Let us consider the below table, Table Data: In the above table, we have common fields like Work_Order and unit. Here every time a new record is inserted while inserting TotUnits, ETCUnits, RwUnits. So we need to get them in one row by eliminating null values as we have Work_Order and unit same for three of them as shown below, OUTPUT: Here we get data in a single row for every distinct Work_Order and unit. Query as Follows: declare @units table (     unit varchar(1000) ) declare @result table (     workorder varchar(1000),TotalUnits varchar(1000),ETCUnits varchar(1000),RwUnits varchar(1000),UnitID varchar(1000),Total varchar(1000) ) insert into @units SELECT distinct Unit FROM  te  --Cursor for iterating declare @tableCursor cursor,         @unit varchar(100) set @tableCursor = cursor for select * from @units open @tableCursor fetch next from @tableCursor into @uni...

How to send email from webapi using c#

Image
To Configure or send email from a web application or a web services, we can use the below code.  Here, we are configuring the mail settings in web config file, the configuration includes host,username,password and sender mail id.   Step 1: Add the below code in web-config Step 2:Paste the below code in web API controller         [HttpGet]         [Route("send-email")]         public async Task SendEmail()         {             var message = new MailMessage();             message.To.Add(new MailAddress("Abhinay" + " <" + "receivermailid@gmail.com" + ">"));             message.From = new MailAddress("AbhinayKumar ");             message.Bcc.Add(new MailAddress("AbhinayKumar "));             message.Subject = "test message"; ...

Import Data From Excel file into Sql Server

Image
First, Create or select Database and right-click on the database name, then select Tasks-> Import Data as shown below Then, Below Screen Shows up Click on Next, Here, from data source dropdown select Microsoft Excel as we are importing data from excel file Then below screen shows up, Here we need to choose the excel file and select the excel version and click on next. Then below screen shows up, Here we need to choose the destination where we need to insert the data. In my case, I am trying to insert it into the local database so I am selecting SQL Server Native Client 11.0 and select the authentication mode and select the database where you want to insert as shown below and click on next. Then below screen shows up, Click on next, then below screen shows up Here You can change the table name if you want or else click on next, Select Run Immediately, then click on next then the data will be inserted into the selected database.

How to return only Json response from webapi method using c#?

Paste the below code in WebApiConfig.cs file, config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); config.Formatters.Remove(config.Formatters.XmlFormatter);

Implement Azure Ad Authentication In webapi c#

Install-Package Microsoft.AspNet.WebApi Install-Package Microsoft.AspNet.WebApi.Owin Install-Package Microsoft.Owin.Host.SystemWeb Install-Package Microsoft.Owin.Security.ActiveDirectory Install-Package Microsoft.Net.Http Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory

How to get row data in kendo grid when button is clicked

Let us consider their is a edit button in kendo  grid as shown below ex:           {                     command: [{                         name: "edit",                         className: "btn-Edit",                         text: "Edit", click: btnEdit                     }], title: "Action", media: "(min-width: 500px)", width: "220px"                 } Now In btnEdit() function we get the button clicked row data using jquery as shown, ex:      function btnEdit(e) {             var grid = $("#gridName").getKendoGrid();             var item = grid.d...