Tuesday 16 December 2014

Adding multiple colors to single cell content using j script in asp.net

Here i bind data to GridView from database
Default.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Customer">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblCustomer" Text='<%# Eval("Customer") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ContactName">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblContactName" Text='<%# Eval("ContactName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ContactTitle">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblContactTitle" Text='<%# Eval("ContactTitle") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code in Default.aspx.cs
SqlConnection con = new SqlConnection("Data Source=INHYDSPARSHIMON\\SQL2012;Initial Catalog=SMPL;User ID=sa;Password=******");
            SqlDataAdapter da = new SqlDataAdapter("Select Customer,ContactName,ContactTitle from tbl_Customer", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "tbl");
            GridView1.DataSource = ds;
            GridView1.DataBind();

//Call javascript function in code behind
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "getdata();", true); 

Sample.js
function getdata() {
    var obj = document.getElementById('GridView1_lblContactName_0').innerText;
    var slp = [];
    slp = obj.split(' ');
    slp[0].fontcolor("green");
    for (var i = 0; i < slp.length; i++) {
        document.getElementById('GridView1_lblContactName_0').innerHTML = slp[0].fontcolor("green");
        document.getElementById('GridView1_lblContactName_0').innerHTML += slp[1].fontcolor("red");
    }
}
Output

            










Wednesday 19 November 2014

Bind data to autocomplete TextBox using jquery in asp.net

Code in Default.aspx
<body>
<form id="form1" runat="server">
<div>
Name:
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</div>
</form>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#txtCity").autocomplete({
source: function (request, response) {
var param = { cityName: $('#txtCity').val() };
$.ajax({
url: "Default.aspx/GetEmployeeName",
data: JSON.stringify(param),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
console.log(data);
response($.map(data.d, function (item) {
return {
value: item
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2//minLength as 2, it means when ever user enter 2 character in TextBox the                         AutoComplete method will fire and get its source data.
});
});
</script>
</body>

Code in Default.aspx.cs
[WebMethod]
public static List<string> GetEmployeeName(string cityName)
{
 List<string> empResult = new List<string>();
 using (SqlConnection con = new SqlConnection(@"Data Source=SHANKAR\SQL2008R2;Initial              Catalog=SMPL;Persist Security Info=True;User ID=sa;Password=********"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select EmployeeName from Employee where EmployeeName LIKE                         ''+@SearchEmpName+'%'";
cmd.Connection = con;
 con.Open();
cmd.Parameters.AddWithValue("@SearchEmpName", cityName);
SqlDataReader dr = cmd.ExecuteReader();
 while (dr.Read())
{
 empResult.Add(dr["EmployeeName"].ToString());
}
con.Close();
return empResult;
}
}
}

Wednesday 23 April 2014

ASP.NET Cache


One of the most important factors in building high-performance, scalable Web applications is the ability to store items, whether data objects, pages, or parts of a page, in memory the initial time they are requested. You can cache, or store, these items on the Web server or other software in the request stream, such as the proxy server or browser. This allows you to avoid recreating information that satisfied a previous request, particularly information that demands significant processor time or other resources. ASP.NET caching allows you to use a number of techniques to store page output or application data across HTTP requests and reuse it.

ASP.NET provides two types of caching that you can use to create high-performance Web applications.

The first is output caching, which allows you to store dynamic page and user control responses on any HTTP 1.1 cache-capable device in the output stream, from the originating server to the requesting browser. On subsequent requests, the page or user control code is not executed; the cached output is used to satisfy the request.
he second type of caching is application data caching, which you can use to programmatically store arbitrary objects, such as application data, in server memory so that your application can save the time and resources it takes to recreate them.

sending email along with database values as attachment in c#

try
    {
      byte[] bytes;
      MailMessage email = new MailMessage();

      email.From = new MailAddress("<sourcr email address>");
      email.To.Add("<destination email address>");
      email.Subject = "Mail Testing";
      email.Body = "You are Successfully Created Profile......";
      string constr =         ConfigurationManager.ConnectionStrings["fishbowlConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {                  
                    cmd.CommandText = "SELECT UserName FROM Email";
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {                      
                        sdr.Read();
                        bytes =         Encoding.ASCII.GetBytes(sdr["UserName"].ToString());                       
                        MemoryStream pdf = new MemoryStream(bytes);
                        Attachment data = new Attachment(pdf, "example.txt", "text/plain");
                        email.Attachments.Add(data);
                    }
                    con.Close();
                }
            }
            SmtpClient smtp = new SmtpClient();
            smtp.UseDefaultCredentials = false;

            smtp.Credentials = new System.Net.NetworkCredential("<source email address>", "<source email password>");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Send(email);
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.ToString());
        }

Friday 7 February 2014

Creating List in share point 2010

Step 1 : go to site action,select more options
Step 2 : Click on List Label in left side panel ,Select custom List,Name it as EmpDetails,Click on Create

Step 3 : For creating column details, Click on List Settings Button
Step 4 : With in the window Click on Create column link
Step 5 : one dialog box will appear , Enter column name and data type for it

Step 6 : Click on OK button.Now Empty List is Created along Column name
Step 7 : For Creating more columns follow step 5 and 6
Step 8 : For uploading data to EmpDetails empty list, Click Upload(+) button for inserting data in List.
Step 9 : Created List along with data.