Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts
What is difference between web.config and app.config files in .Net Applications

What is difference between web.config and app.config files in .Net Applications

Introduction:

In this article i will explain what are the differences between web.config and app.config files. What information can we able to store in this configuration files.

Description:

In previous articles I explained validate email address using  Dropdownlist validation in Asp.net Using RequiredField validator  In ASP.Net And Many Article Relating to Validation Now I Will   Explained  differences between web.config and app.config files  In Asp.net.

These configuration files xml files that can be changed as need by developer. We can say these files are settings files or global files for our applications. Generally these files contains application settings which needs to be available for all of forms in our application. We could store some globally required information here. If we want to use or interact with these files we have to import namespace called System.Configuration(Windows) and System.Web.Configuration(web). Different types of configuration files available in .Net. You can find it in MSDN.


Web.Config:

Web.Config files are purely for web applications.
This file is automatically created when we create new web projects.
we can have many web.config files in web project where each folder can contain single web.config file only.
In this files we can able to store global information which can be accessed based on location of the
config file. Whenever we change these file automatically website will restart with modified details.


App.Config:

App.Config files are for Windows Application.
This file is not add by default when we create new project. We need to externally add these to file to our project by Right Clicking on project --> Select Add New Item--> Select "Application Configuration file".
Modifying App.Config file will not affect the running application until we restart the application.Only one app.config file is allowed in one project.

Conclusion:

Web.Config and App.Config are used for storing global application settings.Check these NameSpaces for more details.System.Configuration(Windows) and System.Web.Configuration(web).


Get (Read) Connectionstring from App.Config File in C#

Get (Read) Connectionstring from App.Config File in C#



Introduction:

Here I will explain how to read or get connectionstring from app.config file in 
c#. By using System.Configuration reference we can read or get connectionstring from app.config file in windows or console application in c#.



<connectionStrings>
<add name="dbconnection" connectionString="DataSource=KaushikDhameliya;Initial Catalog=Kaushik;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>

string admin= ConfigurationManager.ConnectionStrings["admin"].ConnectionString;

Dynamically Dropdown bind Using C#

Dynamically Dropdown bind Using C#

In this example, I will show on how to Dynamically Dropdown Bind Using C# 


      
      
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT Email_Id, Id FROM Admin_Deposit WHERE           Status = '" + 1 +"'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataTextField = "Email_Id";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataSource = ds;

        DropDownList1.DataBind();

Generate Random Code In C#

Generate Random Code In C#


Generate Random Code In C#




private void generate()


    {


        var chars = "0123456789";


        var stringChars = new char[6];


        var random = new Random();




        for (int i = 0; i < stringChars.Length; i++)


        {


            stringChars[i] = chars[random.Next(chars.Length)];




        }




        var finalString = new String(stringChars);


        ab = finalString.ToString();


     //   lblOutlet_Code.Text = finalString.ToString();


    }





Email Validation in Windows Application C#

Email Validation in Windows Application C#

System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");

//txtmail is name/object of textbox
            if (txtmail.Text.Length > 0){

//rEMail is object of Regex class located in System.Text.RegularExpressions
                if (!rEMail.IsMatch(txtmail.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtmail.SelectAll();
                    e.Cancel = true;
                }
Get Mac Address On C#

Get Mac Address On C#


public string GetMACAddress()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            String sMacAddress = string.Empty;
            foreach (NetworkInterface adapter in nics)
            {
                if (sMacAddress == String.Empty)// only return MAC Address from first card
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    sMacAddress = adapter.GetPhysicalAddress().ToString();
                }
            }
            return sMacAddress;
        }
Add data from Textbox to GridView on button click

Add data from Textbox to GridView on button click

C# Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
namespace Punit
{
    public partial class Generate_Bill : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["admin"].ConnectionString);
        private DataTable myTable = new DataTable();
        public Generate_Bill()
        {
            InitializeComponent();
            this.initialiseTable(this.myTable);
            productbind();
            this.dataGridView1.DataSource = this.myTable;
            this.dataGridView2.DataSource = this.myTable;
        }

        private void productbind()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from Admin_Category", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Admin_Category");
            if (ds.Tables.Count > 0 && ds.Tables["Admin_Category"].Rows.Count > 0)
            {

                comboBox1.DataSource = ds.Tables[0];
                comboBox1.ValueMember = "Category_Id";
                comboBox1.DisplayMember = "Category";
            }
         
            con.Close();
        }

        private void producttype()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from Admin_Product where Category='" + comboBox1.Text + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Admin_Product");
            if (ds.Tables.Count > 0 && ds.Tables["Admin_Product"].Rows.Count > 0)
            {

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.ValueMember = "Product_Id";
                comboBox2.DisplayMember = "Product_Name";
            }
         
            con.Close();
        }
        private void initialiseTable(DataTable table)
        {
            table.Columns.Add(new DataColumn("Sr.No"));

            table.Columns.Add(new DataColumn("Product Name"));

            table.Columns.Add(new DataColumn("Product Type"));

            table.Columns.Add(new DataColumn("Weight"));

            table.Columns.Add(new DataColumn("Rate"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label21.Text = textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataRow rd = this.myTable.NewRow();
            rd[0] = comboBox1.Text;

            rd[1] = comboBox2.Text;

            rd[2] = textBox6.Text;
            rd[3] = textBox5.Text;
            this.myTable.Rows.Add(rd);
            prices();
        }

Popular Posts