Friday, 17 August 2012

How to do join between two Table using LinQ

Join given below tables with LinQ to Sql Technique.








Step 1. Open Visual Studio 2008 and choose Web application. Press Ctrl+Alt+s or go to View Menu and choose Server Explorar.

Step 2. Right Click on Data Connection and choose Add connection Option.



























After that Database connection Dialog Box will be open as given below Image:































Select Your Server name, Database name and Enter you Credentials if it has.

After made of connection your server explorer should show you database and its associated tables as shown in below image:
















Step 3. Go to Project Menu and choose Add New Item option and then choose LinQ to SQL Classes file



After that this file will be included in your Solution Explorer 












Step 4. Double Click on this Sample.dbml file then Object Relation Designer will be displayed as given below image:


Step 5. Drag your Server Explorer Tables into Object Relation Designer Frame as shown in below Image:


Step 6. Below code in your default.aspx.cs page.


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace LinqToSql
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          SampleDataContext dc = new SampleDataContext();
          var ids = from c in dc.EMPs
                    join e1 in dc.CITies on c.ID equals e1.ID  into jeet
                    from j in jeet
                    select new { c.ID, c.NAME, j.CITY_NAME };

           foreach (var v in ids)
           {
               Response.Write(v.ID.ToString() +" "+ v.NAME.ToString()+" "+ v.CITY_NAME.ToString()+"<br>");
           }
        }
    }
}

Finally records will be shown as given below output


Tuesday, 17 July 2012

How to Edit and Save Excel Sheet using .NET


        public void EditExcel()
        {
            try
            {
                string file;
                int row = 1;
                
                openFileDialog1.ShowDialog();
                textBox1.Text = openFileDialog1.FileName;

               
                if (textBox1.Text == "")
                  {
                  Interaction.MsgBox("Please Select File  Path",MsgBoxStyle.Information, "Attendace");
                  textBox1.Focus();
                  return;
                  }
                file = textBox1.Text;
               
                Excel.Application app = new Excel.Application();

                Excel.Workbook workbook;
                app = new Excel.Application();

                app.Visible = false;

                workbook = app.Workbooks.Open(file, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                Excel.Worksheet excel = (Excel.Worksheet)workbook.Sheets.get_Item(1);//Missing.Value, Missing.Value, 1, Excel.XlSheetType.xlWorksheet);         

                excel.Name = "In_&_Out_data";

                // for delete the "A" Index Column
                while (row < excel.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Missing.Value).Row + 1)
                {
                    if (excel.Cells[row, 5].ToString() == "Total : ")
                    {
                        excel.Cells[row, 5] = "";
                    }
                    row++;
                }

                Excel.Range range = (Excel.Range)excel.get_Range("A1", Missing.Value);
                range.EntireColumn.Delete(Missing.Value);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range);


                // Remove the "Total:" string from file



                Filename = "C:\\" + excel.Name + ".xlsx";
                if (System.IO.File.Exists(Filename))
                {
                    System.IO.File.Delete(Filename);
                }

                excel.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, 1, 1, 1, 1, 1, 1);
                //excel.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show("There is Problem to Read and Edit Primary Excel Sheet");
                return;
            }
        }

Wednesday, 23 May 2012

How to set alternative row color of Gridview using jQuery in Asp.net


$(document).ready(function(){
$("#<%=GridView1.ClientID%> tr:even").css("background-color","Lightgrey"); 

$("#<%=GridView1.ClientID%> tr:odd").css("background-color","Green");
});

Tuesday, 22 May 2012

Changing GridView row Color on mouseover using jQuery

Step 1: Download Jquery file from Here:

Step 2: Add the Jquery file and write Jquery as Given Below:

 
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<head runat="server">
    <title>Grid View</title>
    <script type ="text/javascript">
    $(document).ready(function()
              {
              $("#<%=GridView1.ClientID%> tr").hover(function()
                {
                 $(this).css("background-color", "Lightgrey");
                },
                function()
                {
                $(this).css("background-color", "#ffffff");
                });
              });s
    </script>



Customise AJAX Using JQuery

Step 1: Download Jquery file from Here

Step 2 : Add the Given Below Code in Your Source Page Named Mydata.aspx.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Mydata.aspx.cs" Inherits="Mydata" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type ="text/javascript">
$(document).ready(function(){
  $("#Button1").click(function(){
    $.ajax({url:"AjaxServer.aspx?id=1",success:function(result){
      $("#GridData").html(result);
    }});
  });
});
</script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="FetchDGContents();"/>

    </ContentTemplate>
         </asp:UpdatePanel>
                
<div id = "GridData">
   
    </div>
    </div>
        </form>


</body>
</html>

Step 3: Add a page named AjaxServer.aspx
Step 4: Write Code in AjaxServer.aspx.cs

public partial class AjaxServer : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(@"Your Connection String");
   
     
protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        //string XmlString;
        SqlDataAdapter da = new SqlDataAdapter("select * from Emp",cn);
        DataSet ds = new DataSet();
        da.Fill(ds, "XmlTable");
        ConvertHTML(ds);
    }



    public void ConvertHTML(DataSet ds)
    {

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                string srtMsg;
                // instantiate a datagrid 
                DataGrid dg = new DataGrid();
                dg.DataSource = ds.Tables[0];
                dg.HeaderStyle.BackColor = System.Drawing.Color.Gray;
                dg.HeaderStyle.Font.Name = "Tahoma";
                dg.HeaderStyle.Font.Size = 12;
                dg.HeaderStyle.ForeColor = System.Drawing.Color.White;
                dg.DataBind();

                dg.RenderControl(htw);
                srtMsg = sw.ToString();

                ds.Clear();
                Response.Clear();
                Response.ContentType = "text/HTML";
                Response.Write(srtMsg);
                Response.End();

            }
        }
    }
   
}