Saturday, 31 December 2011

auto increase value from sql server

 private void PackingIDFill()
        {//----Fill Packing Id AutoGen

            string msid = " SELECT ISNULL(MAX(PackingId),0) FROM dbo.PackingList";
            SqlDataAdapter da = new SqlDataAdapter(msid, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Int32 a = Convert.ToInt32(dt.Rows[0][0].ToString());
            a++; ;
            txtPackingId.Text = a.ToString();
        }

Friday, 30 December 2011

Crystal report use multipal table and SubCrystal report

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 CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace PIS_Billing.Packing.PackingList_Report
{
    public partial class reporton : Form
    {
        public reporton()
        {
            InitializeComponent();
        }
        string PackingId;
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PIS_Billing_Connection"].ToString());
        //----tanking value main form to report form
        public string _textBox
        {
            set { label1.Text = value;
            PackingId = value;
            }
        }
        private void reporton_Load(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                DataTable dt1 = MyPackingList().Copy();
                DataTable dt2 = SpPackingList().Copy();

                DataTable dt3 = Summary_Vw().Copy();


                ds.Tables.Add(dt1);
                ds.Tables.Add(dt2);
                ds.Tables.Add(dt3);


                ReportDocument myreport = new ReportDocument();
                myreport.Load("D:\\PIS_Billing\\PIS_Billing\\PIS_Billing\\Packing\\PackingList_Report\\packingListReport.rpt");


                myreport.SetDataSource(ds);

                crystalReportViewer1.ReportSource = myreport;



            }
            catch (Exception)
            {

                //throw;
            }

           
        }
        protected DataTable MyPackingList()
        {
            string Query = "SELECT * FROM dbo.PackingList WHERE PackingId='" + PackingId + "' ";
            SqlDataAdapter da = new SqlDataAdapter(Query, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "PackingList");
            DataTable Pack = ds.Tables[0];
            return Pack;
        }
        protected DataTable SpPackingList()
        {
            string Query = "SELECT DISTINCT SpId,Specification FROM dbo.PackingList WHERE PackingId='" + PackingId + "'";
            SqlDataAdapter da = new SqlDataAdapter(Query, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "SpecificationInfo");
            DataTable SpecificationInfo = ds.Tables[0];
            return SpecificationInfo;
        }
        private DataTable Summary_Vw()
        {
            string Query = "SELECT * FROM pack_Summary_Vw WHERE PackingId='" + PackingId + "'";
            SqlDataAdapter da = new SqlDataAdapter(Query, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Pack_Summary_VW");
            DataTable Summary_Vw = ds.Tables[0];
            return Summary_Vw;
        }
       
    }
}

Monday, 26 December 2011

sql connection class for asp.net project

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataUtility
/// </summary>
public class DataUtility
{
    # region All Module level variables
    SqlDataAdapter Da;
    SqlConnection Con;
    SqlCommand Cmd;
    DataSet ds;

    # endregion
    public DataUtility()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private void OpenConnection()
    {
        if (Con == null)
        {
            Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PIS_Billing_Connection"].ToString());
        }
        // lets open the connection

        if (Con.State == ConnectionState.Closed)
        {
            Con.Open();
        }

        Cmd = new SqlCommand();

        //Set active connection with the command object

        Cmd.Connection = Con;

    }



    private void CloseConnection()
    {
        if (Con.State == ConnectionState.Open)
        {
            Con.Close();
        }
    }
    /// <summary>
    /// This is to release the memory from the connection object
    /// </summary>
    private void DisposeConnection()
    {
        if (Con != null)
        {
            Con.Dispose();
            Con = null;
        }
    }
    public int ExecuteSql(String Sql)
    {
        OpenConnection();
        //int Result;
        //Set command object properties

        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = Sql;
        Cmd.CommandTimeout = 3000;

        // Now call the method

        int Result = Cmd.ExecuteNonQuery();

        CloseConnection();
        DisposeConnection();

        return Result;
    }
    public bool IsExist(string sql)
    {

        OpenConnection();
        //now set command object properties
        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = sql;
        Cmd.CommandTimeout = 2000;

        //Execute Command object method
        int Result = (int)Cmd.ExecuteScalar();


        CloseConnection();
        DisposeConnection();

        if (Result > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public SqlDataReader GetDataReader(String Sql)
    {
        OpenConnection();
        SqlDataReader dReader;
        Cmd.CommandType = CommandType.Text;
        Cmd.CommandText = Sql;
        Cmd.CommandTimeout = 1000;

        dReader = Cmd.ExecuteReader();
        //CommandBehavior.CloseConnection
        return dReader;
    }
    public DataTable GetDataTable(String sql)
    {
        DataTable dt = new DataTable();
        try
        {
            OpenConnection();
          

            Da = new SqlDataAdapter();
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = sql;
            Cmd.CommandTimeout = 2000;

            Da.SelectCommand = Cmd;
            //ds = new DataSet();
            Da.Fill(dt);
            //dt = ds.Tables["table"];
            CloseConnection();
            return dt;
        }
        catch
        {
            return dt;
        }
    }
    public DataTable returnDataTable(string sql)
    {
        //Con.Open();
        ds = new DataSet();
        Da = new SqlDataAdapter(sql, Con);
        Da.Fill(ds, "datatable");
        DataTable dt = new DataTable();
        dt = ds.Tables["datatable"];
        Con.Close();
        return (dt);
    }
}

datatable create On fly and datatable Fill data by funcation

  private DataTable CreateDataTable()
        {//---Create datatable with datatype
            DataTable myDataTable = new DataTable();

            DataColumn myDataColumn;
            ///id//Specification/SpId/SelectBK/QtyGWt/Unit//TotalLess/Netwt
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Int32");
            myDataColumn.ColumnName = "id";
            myDataTable.Columns.Add(myDataColumn);


            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Specification";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "SpId";
            myDataTable.Columns.Add(myDataColumn);

         

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "SelectBK";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Double");
            myDataColumn.ColumnName = "QtyGWt";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Unit";
            myDataTable.Columns.Add(myDataColumn);


            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Double");
            myDataColumn.ColumnName = "TotalLess";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Double");
            myDataColumn.ColumnName = "Netwt";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "NUnit";
            myDataTable.Columns.Add(myDataColumn);


            return myDataTable;
        }

then use this data table call this on load form
datatable GridTable=new Datatable();
Gridtable=CreateDataTable();

Then You want to fill the value in table then use

AddDataToTable(this.ddlSpecification.Text.ToString(), this.ddlSpecification.SelectedValue.ToString(), this.ddlSelectBK.Text.ToString(), this.txtQTYgrosswt.Text.Trim(), this.ddlUnit.Text.Trim(), this.txtLessWt1.Text.Trim(), this.txtLessWt2.Text.Trim(), this.txtLessWt3.Text.Trim(), this.txtLessWt4.Text.Trim(), this.txtLessWt5.Text.Trim(), this.txtTotalLessWt.Text.Trim(), this.txtNetWt.Text.Trim(), this.ddlNUnit.Text.Trim(), GridTable);


above function pass the value in table  after that use blow funation


 private void AddDataToTable(string Specification, string SpId, string SelectBK, string QtyGWt, string Unit, string lesswt1, string lesswt2, string lesswt3, string lesswt4, string lesswt5, string TotalLess, string Netwt, string NUnit, DataTable myTable)
        {
            try
            {
                DataRow row;
                row = myTable.NewRow();
                //row["id"] = Guid.NewGuid().ToString();

                row["id"] = myTable.Rows.Count + 1;
                row["Specification"] = Specification;
                row["SpId"] = Convert.ToInt32(SpId);
                row["SelectBK"] = SelectBK;
                row["QtyGWt"] = QtyGWt;
                row["Unit"] = Unit;
                row["Lesswt1"] = lesswt1; row["Lesswt2"] = lesswt2; row["Lesswt3"] = lesswt3; row["Lesswt4"] = lesswt4; row["Lesswt5"] = lesswt5;
                row["TotalLess"] = TotalLess;
                row["Netwt"] = Netwt;
                row["NUnit"] = NUnit;
                myTable.Rows.Add(row);
            }
            catch (Exception)
            {
               
               // throw;
            }

          

        }

Thanks u for use this code any problem mail me
mhshry@gmail.com








Saturday, 24 December 2011

Repater on windows from

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;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Billing;Integrated Security=True");
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillRepeater();
            fillRepeater2();
        }
        private void fillRepeater()
        {
            string str = "Select distinct Specification from packingList where packingId=8";
            SqlDataAdapter da = new SqlDataAdapter(str,con);
            DataTable dt = new DataTable();
            DataSet1 ds = new DataSet1();
            da.Fill(dt);
         
                Int32 gh = dt.Rows.Count;
         
                label1.DataBindings.Add("Text", dt, "Specification");
                dataRepeater1.DataSource = dt;
               
               
          
        }
        private void fillRepeater2()
        {
           

            string str = "Select * from packingList where packingId=8 and Specification='" + label1.Text+ "'";
            SqlDataAdapter da = new SqlDataAdapter(str, con);
            DataTable dt = new DataTable();
            da.Fill(dt);

            label2.DataBindings.Add("Text", dt, "ListId");
            label3.DataBindings.Add("Text", dt, "SpType");
            label4.DataBindings.Add("Text", dt, "Quantity");
            label5.DataBindings.Add("Text", dt, "Unit");
            label6.DataBindings.Add("Text", dt, "CrossWt");
            textBox1.DataBindings.Add("Text", dt, "NetWt");
             
        }

    }
}