How to learn c# and how to learn connection c# to sql database

How to learn c# and how to learn connection c# to sql database

Breaking


Wednesday, February 26, 2020

February 26, 2020 1
Adds minutes with dates

string Estimated_LoadDate = "26-2-2020 00:00:00";

DateTime dd = DateTime.Now;
dd = Convert.ToDateTime(Estimated_LoadDate).AddMinutes(TotalMin);

Estimated_LoadDate = dd.ToString();
February 26, 2020 0

Tuesday, December 24, 2019

December 24, 2019 0
How to database connection to C# to Oracle 
public OracleConnection GetConnection_Oracle()
    {
        string connStr = "Data Source=(DESCRIPTION=(ADDRESS_LIST="
        + "(ADDRESS=(PROTOCOL=TCP)(HOST=Server IP Address)(PORT=Server Port Number)))"
        + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=etds)(INSTANCE_NAME=etds)));"
        + "User Id=redfd;Password=dfdf;";
        OracleConnection conn = new OracleConnection(connStr);
        return conn;
    }

    public DataTable GetDataTable_Oracle(string query, out string ERROR)
    {
        OracleConnection dbCon = GetConnection_Oracle();
        dbCon.Open();
        DataTable dt = new DataTable();

        OracleCommand command = new OracleCommand();
        command.Connection = dbCon;
        command.CommandType = CommandType.Text;
        command.CommandText = query;
        command.CommandTimeout = 300;
        try
        {
            OracleDataReader dr = command.ExecuteReader();
            ERROR = String.Empty;
            dt.Load(dr);
        }
        catch (Exception ex)
        {
            //ERROR = ex.ToString();
            ERROR = "";
        }
        finally
        {
            dbCon.Close();
        }
        return dt;
    }
 

 
December 24, 2019 0
Database Connection c# to mssql

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DIGISHOP.App_Data { public class clsDataProcess { public SqlConnection getConnection() { //string connStr = @"data source=(local);database='Database';Connection Timeout=3000;Integrated Security=True"; string connStr = @"data source=SQL14;database=Database;uid=userid;password=password"; SqlConnection conn = new SqlConnection(connStr); return conn; } public DataTable GetDataTable(string query, out string ERROR) { SqlConnection dbCon = getConnection(); dbCon.Open(); DataTable dt = new DataTable(); SqlCommand command = new SqlCommand(); command.Connection = dbCon; command.CommandType = CommandType.Text; command.CommandText = query; try { SqlDataReader dr = command.ExecuteReader(); ERROR = String.Empty; dt.Load(dr); } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return dt; } public int ExecuteSql(string query, out string ERROR) { int result = 0; SqlConnection dbCon = null; try { dbCon = getConnection(); dbCon.Open(); SqlCommand command = new SqlCommand(query, dbCon); var ret = command.ExecuteScalar(); if (ret.GetType() != typeof(DBNull)) result = Int32.Parse(ret.ToString()); else result = 0; ERROR = String.Empty; } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return result; } public string UpdateCommand(string strSQl, out string ERROR) { string rt = ""; SqlConnection dbCon = null; try { dbCon = getConnection(); dbCon.Open(); SqlCommand sqlCmd = new SqlCommand(strSQl, dbCon); sqlCmd.ExecuteNonQuery(); ERROR = String.Empty; } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return rt; } } }

Saturday, July 27, 2019

About c#

July 27, 2019 0
About c#
c# is a very powerful language. It is very easy and comfortable for beginner programmer. Whether you are an experienced programmer or not,But for everyone who wishes to learn the C# programming language. C# is a hybrid of C and C++, it is a Microsoft programming language developed to compete with Sun's Java language. C# is an object-oriented programming language used with XML-based Web services on the .NET platform and designed for improving productivity in the development of Web applications.   


Language paradigms: Object-oriented program...

Developers: Microsoft Corporation.

 First, you should ensure that appropriate hardware and software requirements for developing a C# application have been fulfilled. A simple C# console application can be developed on a system with 32 MB or 64 MB of RAM. The only factor is that the installation of the .NET SDK will take too much time on a system with this configuration. If using Visual Studio .NET, then I recommend that you install on a system having 128 MB of RAM or higher for faster compilation and execution of projects.
In regard to software requirements, the .NET Framework applications can be developed, tested, and deployed under the following operating environments
  • Microsoft Windows "Longhorn"
  • Microsoft Windows Server 2003
  • Microsoft Windows XP Professional
  • Microsoft Windows XP Home
  • Microsoft Windows 2000   
I'll tell you, how to learn C# Step by step


First we will learn how to take the C Sharp form. Click windows start button then click visual studio. when open visual studio then click file→New→Project then open new window and click Installed→Templates→Windows. Right side panel click windows form application.We are project name write specific work related name and save file target drive. when we are design our form then field value wise control get. First we are design login form.we are write anything then get label control and write randomly anything get text box and show image then use image box.





Design Code Back end
=================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DIGISHOP.App_Data;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using System.Management;

namespace DIGISHOP.LogInForm
{
    public partial class UserLogin : Form
    {
        string error = String.Empty;
        clsDataProcess cls = new clsDataProcess();
        DataTable dt = null;
        string sql = "";
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);

        public UserLogin()
        {
            ManagementClass mc = new ManagementClass("win32_processor");
            ManagementObjectCollection moc = mc.GetInstances();
            String Id = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                Id = mo.Properties["processorID"].Value.ToString();
                break;
            }
           if (Id == "BFEBFBFF0001067A")
            {
                int oldversion = 0;
                int version = 1;
                sql = @"SELECT        VersionControl
            FROM            (SELECT        TOP (100) PERCENT VersionControl
            FROM            dbo.LoginPageDesign
            WHERE        (VersionControl IS NOT NULL)
            ORDER BY CompanyID DESC) AS f";
                dt = cls.GetDataTable(sql, out error);
                if (dt.Rows.Count > 0)
                {
                    oldversion = Convert.ToInt32(dt.Rows[0]["VersionControl"].ToString());
                }
                if (oldversion <= version)
                {

                    sql = "UPDATE LoginPageDesign SET VersionControl='" + version + "'";
                    cls.UpdateCommand(sql, out error);
                }

                sql = "SELECT * FROM LoginPageDesign WHERE VersionControl='" + version + "'";
                dt = cls.GetDataTable(sql, out error);
                if (dt.Rows.Count > 0)
                {

                    InitializeComponent();
                    this.FormBorderStyle = FormBorderStyle.None;
                    Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));

                    sql = "SELECT * FROM LoginPageDesign";
                    dt = cls.GetDataTable(sql, out error);
                    if (dt.Rows.Count > 0)
                    {
                        lblConpanyName.Text = dt.Rows[0][1].ToString();
                        lblTitle.Text = dt.Rows[0][3].ToString();
                    }
                    try
                    {
                        //string connStr = @"data source=(DataSource Name);database=DataBaseName;Connection Timeout=3000;Integrated Security=True";

                        //SqlConnection objConn = new SqlConnection(connStr);
                        SqlConnection objConn = cls.getConnection();
                        objConn.Open();
                        SqlCommand objComm = new SqlCommand();
                        objComm.Connection = objConn;
                        objComm.CommandText = "SELECT CompanyLogo FROM LoginPageDesign";// WHERE  ID= '" + ownEmpID + "'";
                        objComm.Connection = objConn;
                        objComm.CommandType = CommandType.Text;

                        SqlDataReader r = objComm.ExecuteReader();
                        if (r.Read())
                        {
                            try
                            {
                                byte[] b = (byte[])r[0];
                                MemoryStream st = new MemoryStream();
                                st.Write(b, 0, b.Length);
                                Image i = Image.FromStream(st);
                                pictureBox.Image = i;

                            }
                            catch
                            {
                                pictureBox.Image = null;

                            }
                        }
                        objConn.Close();
                    }
                    catch (SqlException sqle)
                    {
                        Console.WriteLine(sqle.Message);
                    }
                    sql = @"SELECT        USER_ID, USER_NAME, USER_DESIGNATION, USER_PASSWORD, USER_ORGANIZATION_ID, UserActivity
                    FROM dbo.UserList";
                    dt = cls.GetDataTable(sql, out error);
                    cmbLogingUser.DataSource = dt;
                    cmbLogingUser.DisplayMember = "USER_NAME";
                    cmbLogingUser.ValueMember = "USER_ID";
                    cmbLogingUser.SelectedValue = 1;

                    cmbLogingUser.Focus();
                    cmbLogingUser.Select();

                    //txtLoginUserName.Focus();
                    //txtLoginUserName.Select();
                }
                else
                {
                    MessageBox.Show("Version fail, Please contract your vendor.");
                    //this.Close();
                }
            }
            else
            {
                MessageBox.Show("Duplicate Piracy, Please contract vendor.");
                //this.Close();
            }
        }
        #region Member Variable
        bool b = false;
        public static string UserName = "";
        public static string UserRole = "";
        public static int UserID = 0;
        #endregion
        private void Login_Click(object sender, EventArgs e)
        {
            //Mainfrm ObjForm1 = new Mainfrm();
            //ObjForm1.Show();           
            //if (txtLoginUserName.Text != "")
            //{
            if (Convert.ToInt32(cmbLogingUser.SelectedValue)>0)
            {
                
                if (txtPassword.Text.Trim().ToString() != "")
                {
                    //string userid = txtLoginUserName.Text;
                    string userid = cmbLogingUser.Text;
                    string Pass = txtPassword.Text.Trim().ToString();
                    sql = @"SELECT     USER_ID, USER_NAME, USER_PASSWORD FROM  dbo.UserList WHERE     (USER_NAME = '" + userid + "') AND (USER_PASSWORD = '" + Pass + "') AND (UserActivity = 1)";
                    dt = cls.GetDataTable(sql, out error);
                    //if (txtPassword.Text != dtt.ToString())
                    if (dt.Rows.Count > 0)
                    {
                        UserName = dt.Rows[0]["USER_NAME"].ToString();
                        UserID = Convert.ToInt32(dt.Rows[0]["USER_ID"].ToString());
                        Hide();
                        //this.ShowInTaskbar = false;
                        Mainfrm ObjForm1 = new Mainfrm();
                        ObjForm1.Show();
                    }
                    else
                    {
                        MessageBox.Show("Did you forget your password? Please type your password again. Be sure to use the correct Uppercase and Lowercase Letters.", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtPassword.Text = "";
                        cmbLogingUser.Focus();
                        cmbLogingUser.Select();

                        //txtLoginUserName.Focus();
                        //txtLoginUserName.Select();
                    }

                }
                else
                {
                    MessageBox.Show("Please type your passward");
                    txtPassword.Focus();
                    txtPassword.Select();
                }

            }
            else
            {
                MessageBox.Show("Please select user name");
                cmbLogingUser.Focus();
                cmbLogingUser.Select();
                //txtLoginUserName.Focus();
                //txtLoginUserName.Select();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Process[] pname = Process.GetProcessesByName(AppDomain.CurrentDomain.FriendlyName.Remove(AppDomain.CurrentDomain.FriendlyName.Length - 1));
            if (pname.Length > 0)
            {
                pname.Where(p => p.Id != Process.GetCurrentProcess().Id).First().Kill();
            }
        }

        private void txtLoginUserName_Enter(object sender, EventArgs e)
        {
            txtLoginUserName.BackColor = Color.Cyan;
        }

        private void txtLoginUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                e.SuppressKeyPress = true;
                if (txtLoginUserName.Text.Trim() != "")
                {
                    txtPassword.Focus();
                    txtPassword.Select();
                }
                else
                {
                    MessageBox.Show("Please inpur user");
                    txtLoginUserName.Focus();
                    txtLoginUserName.Select();
                }
            }
        }

        private void txtLoginUserName_Leave(object sender, EventArgs e)
        {
            txtLoginUserName.BackColor = Color.White;
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            txtPassword.BackColor = Color.Cyan;
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                e.SuppressKeyPress = true;
                if (txtPassword.Text.Trim() != "")
                {
                    Login.Focus();
                    Login.Select();
                }
                else
                {
                    MessageBox.Show("Please inpur password");
                    txtPassword.Focus();
                    txtPassword.Select();
                }
            }
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            txtPassword.BackColor = Color.White;
        }

        private void cmbLogingUser_Enter(object sender, EventArgs e)
        {
            cmbLogingUser.BackColor = Color.Cyan;
        }

        private void cmbLogingUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                e.SuppressKeyPress = true;
                if (Convert.ToInt32(cmbLogingUser.SelectedValue) > 0)
                {
                    txtPassword.Focus();
                    txtPassword.Select();
                }
                else
                {
                    MessageBox.Show("Please select user");
                    cmbLogingUser.Focus();
                    cmbLogingUser.Select();
                }
            }
        }

        private void cmbLogingUser_Leave(object sender, EventArgs e)
        {
            cmbLogingUser.BackColor = Color.White;
        }
    }
}


When Design complete then we are learn connect database. 

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DIGISHOP.App_Data { public class clsDataProcess { public SqlConnection getConnection() { //string connStr = @"data source=(local);database='Database';Connection Timeout=3000;Integrated Security=True"; string connStr = @"data source=SQL14;database=Database;uid=userid;password=password"; SqlConnection conn = new SqlConnection(connStr); return conn; } public DataTable GetDataTable(string query, out string ERROR) { SqlConnection dbCon = getConnection(); dbCon.Open(); DataTable dt = new DataTable(); SqlCommand command = new SqlCommand(); command.Connection = dbCon; command.CommandType = CommandType.Text; command.CommandText = query; try { SqlDataReader dr = command.ExecuteReader(); ERROR = String.Empty; dt.Load(dr); } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return dt; } public int ExecuteSql(string query, out string ERROR) { int result = 0; SqlConnection dbCon = null; try { dbCon = getConnection(); dbCon.Open(); SqlCommand command = new SqlCommand(query, dbCon); var ret = command.ExecuteScalar(); if (ret.GetType() != typeof(DBNull)) result = Int32.Parse(ret.ToString()); else result = 0; ERROR = String.Empty; } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return result; } public string UpdateCommand(string strSQl, out string ERROR) { string rt = ""; SqlConnection dbCon = null; try { dbCon = getConnection(); dbCon.Open(); SqlCommand sqlCmd = new SqlCommand(strSQl, dbCon); sqlCmd.ExecuteNonQuery(); ERROR = String.Empty; } catch (Exception ex) { ERROR = ex.ToString(); } finally { dbCon.Close(); } return rt; } } }



Main Page Add Panel


private void addPanel() { //UserLoginForm ObjUserLoginForm = new UserLoginForm(); StatusBar mainStatusBar = new StatusBar(); StatusBarPanel statusPanel = new StatusBarPanel(); // Set first panel properties and add to StatusBar statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken; statusPanel.Text = "Application started."; statusPanel.ToolTipText = "Last Activity"; statusPanel.AutoSize = StatusBarPanelAutoSize.Contents; mainStatusBar.Panels.Add(statusPanel); StatusBarPanel unitName = new StatusBarPanel(); // Set first panel properties and add to StatusBar unitName.BorderStyle = StatusBarPanelBorderStyle.Sunken; unitName.Text = "Unit Name : " + UserLogin.UserName; unitName.AutoSize = StatusBarPanelAutoSize.Spring; mainStatusBar.Panels.Add(unitName); StatusBarPanel userName = new StatusBarPanel(); // Set first panel properties and add to StatusBar userName.BorderStyle = StatusBarPanelBorderStyle.Sunken; userName.Text = "User Name :" + UserLogin.UserName; userName.AutoSize = StatusBarPanelAutoSize.Spring; mainStatusBar.Panels.Add(userName); StatusBarPanel department = new StatusBarPanel(); // Set first panel properties and add to StatusBar department.BorderStyle = StatusBarPanelBorderStyle.Sunken; //department.Text = "Department :" + UserLoginForm.Department; department.AutoSize = StatusBarPanelAutoSize.Spring; mainStatusBar.Panels.Add(department); StatusBarPanel userrole = new StatusBarPanel(); // Set first panel properties and add to StatusBar userrole.BorderStyle = StatusBarPanelBorderStyle.Sunken; userrole.Text = "User Role : " + UserLogin.UserRole; userrole.AutoSize = StatusBarPanelAutoSize.Spring; mainStatusBar.Panels.Add(userrole); StatusBarPanel datetimePanel = new StatusBarPanel(); // Set second panel properties and add to StatusBar datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised; datetimePanel.ToolTipText = "DateTime: " + System.DateTime.Today.ToString(); datetimePanel.Text = System.DateTime.Today.ToLongDateString(); datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents; mainStatusBar.Panels.Add(datetimePanel); mainStatusBar.ShowPanels = true; Controls.Add(mainStatusBar); }