Posts

Showing posts from March, 2024

Host Angular app INSIDE C# Web Api 2 (as single app) (not .Net Core) and install in server (and browse from outside)

Image
Host Angular inside ANY C# / ASP.NET solution. Table of Content: Web Dev Tools List Create DB (ms-sql) Create API (c# webapi 2) + connect to DB Create Angular project  + connect to API Host the NgApp inside the API Install in Server + connect from internet 1. Web Dev Tools List The list of tools : they are all "Next,  Next, Next...", and they are all on google search, SSMS - Sql Server Management Tool - for SQL (also installs server) VS22 - Visual Studio 2022 Community - dont forget to check Asp.Net dev tools with .Net framewroks 4.6.1-4.8 VSC - Visual Studio Code NodeJS (version compatible to the angular version) Angular (your version) For server install - windows server (2022) with IIS 2. Create DB Open SSMS (type "SQL" in start) There, copy your server name (before click "Connect"): Create a DB and a Table (just right-click -> "New..." and give names) Make sure you have an "Id" columns that is key identity, by double-clicking the

3 Simple ways to retrieve generic SQL data with C# / WebAPI controller (using DataRow)

Image
Full Code in the end! :) Assuming you have some connection string: private static string ConnectionString = ConfigurationManager.AppSettings["myConnStrApp"]; And assuming we are using the simple "SqlDataAdapter", our Method will start with: using (SqlConnection conn = new SqlConnection(ConnectionString)) {       using (SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM " + tblname, conn)) {             DataSet ds = new DataSet("ds");             adp.Fill(ds); We have 2 generic options return (convert to) Json String build json object Lets 1st mention the "standard" way - by class model. That means we need to create a new C# class per sql table, and then create matching parsing mechanism, meaning that the rest of our method contains this (see more in the end): var rows = ds.Tables[0].Rows; for (int i = 0; i < rows.Count; i++) {     var r = rows[i];     t.Add(new TNG() {         Id = int.Parse(r["Id"].ToString()),