Saturday 23 June 2012

Maintain Value In Windows Application In .Net:


Using this coding, we can maintain a value throughout the application and access that value in any form of the application.
Example:
Declare and Define Value:    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
{
    public class MaintainValue
    {
        //User Name
        public static string strUserName;
        public static string UserName
        {
            get
            {
                return strUserName;
            }
            set
            {
                strUserName = value;
            }
        }
        //Pass Word
        public static string strPassword;
        public static string Password
        {
            get
            {
                return strPassword;
            }
            set
            {
                strPassword = value;
            }
        }
    }
}

Access the value:
//Calls the UserName Method of class MaintainValue and assign it to the label.
            lblusername.Text = MaintainValue.UserName;
                                                            

VIRTUAL KEYBOARD IN C# FOR WINDOWS APPLICATION:


If you want to  enable virtual keyboard in our application, first we have to add a namespace

“using  System.Diagnostics;”

private void btnvirtual_Click(object sender, EventArgs e)
        {
            txtUserName.Focus(); //To place the cursor in textbox
            Process prc = Process.Start("OSK.exe");    
        }

 To Kill the Process without using window close button:

foreach (Process p in Process.GetProcessesByName ("Process Name"))
            {
                if (p.ProcessName.StartsWith("ProcessName"))
                    p.Kill();              
            }

Wednesday 13 June 2012

Upload Excel File into Dataset,Datatable or Database using Asp.net and C#


Using this Code We can Take both (.xls) format and (.xlsx) format
  try
        {
            string target = Server.MapPath("~/upload");
            if (fileupload1.HasFile)
            {
               fileupload1.SaveAs(System.IO.Path.Combine(target, fileupload1.FileName));
                string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + target + "\\" + fileupload1.FileName + ";" + "Extended Properties='Excel 12.0;HDR=YES;'";
                string query = String.Format("SELECT * FROM [{0}$]", "Sheet1");
                OleDbDataAdapter da = new OleDbDataAdapter(query, connectionString);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GVexcel.DataSource = ds.Tables[0];
                GVexcel.DataBind();
           
           
            }
        }
      catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally{
        }
if any problem on this  please let me know...

Tuesday 12 June 2012

What is the difference between Convert.Tostring() and Tostring ()


int j =0;
MessageBox.Show(j.ToString());
MessageBox.Show(Convert.ToString(j));
We can convert the integer “j” using “j.ToString()” or “Convert.ToString” so what’s the difference.
The basic difference between them is  Convert.ToString() return string.Empty in case of null object  while “j.ToString()”
ToString() raise exception when the object is null

Create simple Login Page Using Window Application in C#


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 MySql.Data.MySqlClient;
using System.IO;
using System.Configuration;


namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection ObjCon = new MySqlConnection(new Common().GetConnectionString()))
                {
                ObjCon.Open();
                DataSet ds = new DataSet();
                using (MySqlCommand ObjCmd = new MySqlCommand("SP_TBL_SM_Login", ObjCon))
                {
                    ObjCmd.CommandType=CommandType.StoredProcedure;
                    ObjCmd.Parameters.Add("@UserName",MySqlDbType.VarChar,8).Value=txtUserName.Text.Trim();
                    ObjCmd.Parameters.Add("@Password",MySqlDbType.VarChar,8).Value=txtPassword.Text.Trim();
                    MySqlDataAdapter ada = new MySqlDataAdapter(ObjCmd);
                    ada.Fill(ds,"Login");
                    if (ds.Tables["Login"].Rows.Count > 0)
                    {
                        Menu M = new Menu();
                        M.ShowDialog();
                    }
                    else
                        MessageBox.Show("InValid UserName And Password!!!");


            }
                }
            }
                catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                }
        }
    }
}

what is the difference between show and show dialog in c#.Net


These methods are used in Window applications to call one form from another
ShowDialog()
ShowDialog shows activated window and parent form cannot be Accessed by the user.
private void button_Click(object s, EventArgs e)
{
Form1 f=new Form1();
f.ShowDialog();

}

Show()
Show shows the window but other application windows are accessible.
private void button1_Click(object s, EventArgs e)
{
Form2 f=new Form2();
f.ShowDialog();

}