Monday, 25 March 2013

how to bind database into chart control


<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
            <Series>
                <asp:Series Name="Series1" XValueMember="EmpName" YValueMembers="EmpID">
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </ChartAreas>

 </asp:Chart>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
            SelectCommand="SELECT top 10 [EmpName], [EmpID] FROM [tblEmployeeMaster]">
</asp:SqlDataSource>

Friday, 8 March 2013

Sql server Procedure for if Record insert first time then insert else update the table.....

I have table in which I am inserting rows for employee but next time when I want to insert row I don't want to insert again data for that employee just want to update with required columns if it exits there if not then create new row....


Syntax For Procedure:


CREATE PROCEDURE dbo.InsertOrUdpateEmployee
       @ID INT,
       
AS BEGIN
IF NOT EXISTS (SELECT * FROM Employee WHERE ID = @ID)
    INSERT INTO Employee(Col1, ..., ColN)
    VALUES(Val1, .., ValN)

ELSE

    UPDATE Employee
    SET Col1 = Val1, Col2 = Val2, ...., ColN = ValN
    WHERE ID = @ID
END

Tuesday, 12 February 2013

Common Class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Configuration;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Summary description for CommonClass
/// </summary>
public class CommonClass
{
   
     public const string SELECTED_CUSTOMERS_INDEX = "SelectedCustomersIndex";
    string ConnectionString=string.Empty;
   static SqlConnection con;

   public CommonClass()
          {
        ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        con = new SqlConnection(ConnectionString);
          }

    public void SetConnection()
    {
        if (ConnectionString == string.Empty)
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }
        con = new SqlConnection(ConnectionString);
    }

    public DataSet ExecuteProcudere(string procName, Hashtable parms)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        cmd.CommandText = procName;
        cmd.CommandType = CommandType.StoredProcedure;
        if (con == null)
        {
            SetConnection();
        }
        cmd.Connection = con;

        if (parms.Count > 0)
        {
            foreach (DictionaryEntry de in parms)
            {
                cmd.Parameters.AddWithValue(de.Key.ToString(), de.Value);
            }
          
        }
        da.SelectCommand = cmd;
       

        da.Fill(ds);
        return ds;
    }

    public int ExecuteQuery(string procName, Hashtable parms)
    {
        SqlCommand cmd = new SqlCommand();
       
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = procName;
        if (parms.Count > 0)
        {
            foreach (DictionaryEntry de in parms)
            {
                    cmd.Parameters.AddWithValue(de.Key.ToString(), de.Value);
            }

        }
        if (con == null)
        {
            SetConnection();
        }
        cmd.Connection = con;
        if (con.State == ConnectionState.Closed)
            con.Open();

      int result=cmd.ExecuteNonQuery();
      
      return result;

    }


    public int ExecuteQuerywithOutputparams(SqlCommand cmd)
    {          
        if (con == null)
        {
            SetConnection();
        }
        cmd.Connection = con;
        if (con.State == ConnectionState.Closed)
            con.Open();

        int result = cmd.ExecuteNonQuery();

        return result;

    }

    public int ExecuteQueryWithOutParam(string procName, Hashtable parms)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter sqlparam = new SqlParameter();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = procName;
        if (parms.Count > 0)
        {
            foreach (DictionaryEntry de in parms)
            {
                if (de.Key.ToString().Contains("_out"))
                {
                    sqlparam = new SqlParameter(de.Key.ToString(),de.Value);
                    sqlparam.DbType = DbType.Int32;
                    sqlparam.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(sqlparam);
                }
                else
                {
                    cmd.Parameters.AddWithValue(de.Key.ToString(), de.Value);
                }
            }

        }
        if (con == null)
        {
            SetConnection();
        }
        cmd.Connection = con;
        if (con.State == ConnectionState.Closed)
            con.Open();

        int result = cmd.ExecuteNonQuery();

        if (sqlparam != null)
            result = Convert.ToInt32(sqlparam.SqlValue.ToString());
        return result;
    }

    



   


       //  mail sending

    public void mailsend(string mailId, string subject, string body)
    {
        try
        {
            string smtpServer = ConfigurationManager.AppSettings["SmtpServer"].ToString();
            int smtpserverport = Convert.ToInt32(ConfigurationManager.AppSettings["smtpserverport"]);

            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["FromId"], "HIHL-1056", System.Text.Encoding.UTF8);
            mailMsg.To.Add(mailId);
            mailMsg.Subject = subject;
            mailMsg.Body = body;
            mailMsg.BodyEncoding = System.Text.Encoding.UTF8;
            mailMsg.IsBodyHtml = true;
            mailMsg.Priority = System.Net.Mail.MailPriority.High;
            //The SMTP requires Authentication so the credentials has to be sent

            System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential("harish.kumar@envilenz.com", "Hareesh28");



            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Port = smtpserverport;
            client.Host = smtpServer;
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = mailAuthentication;

            object userState = mailMsg;

            client.Send(mailMsg);

        }
        catch (Exception ex)
        {
            

        }


    }


    public int ExecuteNonQuery(string p, Hashtable ht)
    {
        throw new NotImplementedException();
    }

    public DataSet ExecuteProcudere(char p, Hashtable ht)
    {
        throw new NotImplementedException();
    }
}


Friday, 28 December 2012

Rownum in sql server 2008 || How to find the Middle rows in sql server table

SELECT * FROM
(
   SELECT ROW_NUMBER() OVER(ORDER BY   Your_ColumnName) NUM,
   * FROM    YourTable_Name
) A
WHERE NUM >1 AND NUM <5

Monday, 27 August 2012

How to give the Alias Name with space in sql Server 2008



ALTER  procedure [dbo].[Inventory_Alerts]
as
begin

select (select Itemname from TechInventory_ItemName where Id=a.Itemname)as ‘Item Name’ ,DateDiff(dd,getdate(),TentativeAlert)as ‘Remaining Days’ from TechInventory_Create as a where TentativeAlert is not null and DateDiff(dd,getdate(),TentativeAlert)=1


end

Error converting data type nvarchar to date in asp.net


      protected void btnDisplay_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            ArrayList arrlst = new ArrayList();


            string fromdt = txtFromDate.Text.Substring(txtFromDate.Text.Length - 4) + "/"     + txtFromDate.Text.Substring(3, 2) + "/" + txtFromDate.Text.Substring(0, 2);
            string todt = txtTodate.Text.Substring(txtTodate.Text.Length - 4) + "/" + txtTodate.Text.Substring(3, 2) + "/" + txtTodate.Text.Substring(0, 2);


            arrlst.Add(fromdt);
            arrlst.Add(todt);

            DataSet ds = bal.Search_InventoryDetails(arrlst);
            GrdInventory.DataSource = ds.Tables[0];
            GrdInventory.DataBind();
           
        }