1
\$\begingroup\$

I have been working on chatApp (my first app in c#). I used Azure SQL Database, ASP.NET Web API and WinFroms. The app works very well with no errors

Please give me your professional opinion and advice for myapp/ the way thecode was written.

Any advises/articles/tutorials that can help to improve the way i write code ?

Here is my main class on GitHub

Screenshots of my on GitHub

The classes below handles the restful connection with my Azure SQL Database.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Backend.Connector;
using Backend.Models;
namespace Backend.Controllers
{
    public class UserController : ApiController
    {
        // GET: api/User
        public List<User> Get()
        {
            UserDbConnector dbc = new UserDbConnector();
            return dbc.GetUsers();
        }

        // GET: api/User/5
        // GET: api/Person/5
        public User Get(int id)
        {
            return null;
        }


        // POST: api/User
        public void Post(User user)
        {
            UserDbConnector dbc = new UserDbConnector();
            dbc.SaveUser(user);
        }

        // PUT: api/User/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/User/5
        public void Delete(int id)
        {
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Backend.Controllers;
using Backend.Models;
using System.Text;
namespace Backend.Connector
{
       public class UserDbConnector
    {
        private SqlConnection connection;

        public UserDbConnector()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "chat.database.windows.net";
            builder.UserID = "aboualy";
            builder.Password = "****";
            builder.InitialCatalog = "chat";

            try
            {
                connection = new SqlConnection(builder.ConnectionString);
                connection.Open();
            }

            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error in PersonDbConnector(): " + e.Message);
            }
        }



        public void SaveUser(User user)
        { 
            try
            { 
                String sql = "INSERT INTO tables VALUES ('" + user.username + "','" + user.firstname + "','" + user.lastname + "');"; 
                SqlCommand command = new SqlCommand(sql, connection);
                command.ExecuteNonQuery();
            }

            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error in SaveUser(): " + e.Message);
            }
        }




        public List<User> GetUsers()
        {
            List<User> users = new List<User>();
            String sql = "SELECT * from tables";

            try
            {
                SqlCommand command = new SqlCommand(sql, connection);
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    users.Add(new User(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)));
                }
            }

            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error in GetUsers(): " + e.Message);
            }

            return users;
        }




        }

    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.