-->

Friday, June 26, 2015

How to bind DataGrid in WPF using Sql

DataGrid is a way to show the data items of database table in wpf application. So, first to design the database table by using Sql Server. I have a country table, which is mentioned below:

CREATE TABLE [dbo].[country] (
    [CountryId]    INT            IDENTITY (1, 1) NOT NULL,
    [CountryName]  NVARCHAR (MAX) NULL,
    [Countryimage] NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([CountryId] ASC)
);


After design the table you need to insert some data in it. So you can show your table data in wpf application with the help of DataGrid. Make two column in DataGrid, i have done this , you can check by the mentioned below code:

<DataGrid Name="g1" AutoGenerateColumns="False" Margin="110,121,137,57">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="100"/>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>

Here Binding property is directly connected with table columns and Header property assigned by user/Developer.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            binddatagrid();
   

        }

        private void binddatagrid()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["connstudent"].ConnectionString;
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from [Students]";
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("Students");
            da.Fill(dt);

            g1.ItemsSource = dt.DefaultView;
            con.Close();
        }
}
}

With the help of c# code you can connect with Database table, In WPF we have ItemSource property to bind the DataGrid, in other application like asp.net, we have DataSource property.

Saturday, June 20, 2015

Dropdownlist with image in asp.net c#

If you want to make beautiful and attractive web application in asp.net then you think ahead. You can make each control attractive using some other technologies like JQuery, JavaScript etc. Today i am discussing with you on topic Dropdownlist. If you bind the list with simple text then your Dropdownlist not look beautiful but if you add some images with each item then your list becomes beautiful. Lets take a simple example of Dropdownlist with image.

So first of all design database table and imsert some data in it , I have a table , which is mentioned below.

Dropdownlist with image in asp.net c#



Source page

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind DropDownlist With Image</title>
<link href="style/dd.css" rel="stylesheet" />
<script src="Scripts/jquery-1.6.1.min.js"></script>
<script src="Scripts/jquery.dd.js"></script>
<script>
$(document).ready(function(){

try {
$("#DropDownList1").msDropDown();

} catch (e) {
alert(e.message);

}
})
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="26px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="149px">

</asp:DropDownList>
    <br />
    </div>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>
In the above mentioned code we have two ".js" file and one css file, both are used to make the dropdownlist beautiful also insert the image with text.

Code Behind file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Dropdownlistwithimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            Binddropdown();
            BindTitle();

        }

    }

    private void BindTitle()
    {
     
        if(DropDownList1!=null)
        {
            foreach (ListItem li in DropDownList1.Items)
            {
                li.Attributes["title"] = li.Value;
            }
        }
    }

    private void Binddropdown()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from [country]";
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataTextField = "CountryName";
        DropDownList1.DataValueField = "Countryimage";
        DropDownList1.DataSource = ds;
        DropDownList1.DataBind();


    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedValue;
        BindTitle();
    }
}
After bind the Dropdownlist you have to access each item from the list by using DataBound event. Use foreach loop to access all item one by one from the list also apply tooltip on each item by the help of Attribute property. When you apply attribute in BindTitle( ) method on that time JQuery will apply on it so you see the image with text in Dropdownlist.

Download the code

Friday, June 19, 2015

How to access application setting tag of web.config file in asp.net

Today i am writing ado.net code in code behind file, On that time i notice that if i take my connection string in the web.config file then we can access it easily. But how? So i have search in msdn library to access the web.config file then i found ConfigurationManager class which is available in System.Configuration Namespace. Through this class we can get all elements of web.config file. Lets take a simple example.




Web.config file

<appSettings>
    <add key="jacob" value="My Name"/>
   
  </appSettings>

Source Code

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    <asp:Button ID="Button1" runat="server" Height="57px" OnClick="Button1_Click" Text="Get Data from Application Setting" Width="232px" />
    </form>
</body>
</html>

Code Behind Code
using System.Configuration;
 protected void Button1_Click(object sender, EventArgs e)
    {
        String getname = ConfigurationManager.AppSettings["jacob"].ToString();
        Label1.Text = getname;
    }

Here we access AppSettings By the configurationManager class. You can access all property or elements from web.config file by using this class. Now the simple syntax to access them.
ConfigurationManager.elementName

Print the asp.net webpage using button

Introduction

Today i am talking about printing, and how to print the content or you can say selected content in asp.net. If you have a webpage and you want to print this then you have to choose command key (ctrl+p) for that. Also you want to print the selected part then you have to select the text first then you have to use command key. But sometimes your selected page could not printed. So many websites provide the printing facilities on the webpage. Today i am talking about the same topics here.
Lets take a simple example to demonstrate the topic.

Source code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script>
function printpage() {

var getpanel = document.getElementById("<%= Panel1.ClientID%>");
var MainWindow = window.open('', '', 'height=500,width=800');
MainWindow.document.write('<html><head><title>Print Page</title>');
MainWindow.document.write('</head><body>');
MainWindow.document.write(getpanel.innerHTML);
MainWindow.document.write('</body></html>');
MainWindow.document.close();
setTimeout(function () {
MainWindow.print();
}, 500);
return false;

}
</script>
</head>
<body>
    <form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">

<table style="width: 100%;">
<tr>
<td>English Marks</td>
<td>Social Marks </td>
<td>Science Marks</td>
</tr>
<tr>
<td>60</td>
<td>56</td>
<td>23</td>
</tr>
<tr>
<td>56</td>
<td>12</td>
<td>23</td>
</tr>
</table>
</asp:Panel>
    <asp:Button ID="Button1" runat="server" OnClientClick="return printpage();"  Text="Print Page" />
    </form>
</body>
</html>
Code generate the following output:


Print the asp.net webpage using button

In this example i have a panel control with table. Table contains data. Also i have a button control in the page . When we click on it, a javascript function will raised. In this function, first of all get the panel with their id property. Create a window with the help of window.open( ) method also set the width and height of it. Now, write the html code in the window by using write method also write the panel contents in the body section. After that print the window.

Wednesday, June 17, 2015

ToolTip on specific DropdownList item in asp.net c#

Today i am talking about tooltip, First of all i introduce you , what is tooltip? When our mouse cursor move over the object then appear a rectangle shape with text this things known as tooltip. When we work with particular item then easy set the tooltip on it. But when we work on collection then we put some ideas on it. Like first to access each item from the collection then apply tooltip on each accessed item. Today, i want to give a simple example on it. I have a database table with three columns, see below:


Database table(country table)

CREATE TABLE [dbo].[country] (
    [CountryId]    INT            IDENTITY (1, 1) NOT NULL,
    [CountryName]  NVARCHAR (MAX) NULL,
    [Countryimage] NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([CountryId] ASC)
);

First to add some item in the table after then you have to access items. If you want to add items in the table by the help visual studio server explorer then do the following.

Right click on your table name --> Select show table data

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:DropDownList ID="DropDownList1" runat="server"  Height="26px" OnDataBound="DropDownList1_DataBound" Width="142px" DataSourceID="SqlDataSource1" DataTextField="CountryName" DataValueField="CountryId"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

How to design above mentioned code. The answer is :Bind the DropdownList with the SqlDataSource also SqlDataSource Control connect with database table. The above mentioned code automatically generated by the visual studio.

Code Behind Code

protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;

            }
        }
    }

After bind the Dropdownlist you have to access each item from the list by using DataBound event. Use foreach loop to access all item one by one from the list also apply tooltip on each item by the help of Attribute property.

Tuesday, June 16, 2015

How to create array using JQuery also insert item at run time

Array is a collection of similar item in programming language but is case of scripting language all variable treat as a variable type so we don't identify the type of it. In JQuery we have to create array at runtime, like
var array_name=[];
If you want to insert some item in it then use push(var item ) method.



<%@ Page Language="C#" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="Scripts/jquery-1.10.2.js"></script>
<script>
$(function(){
var array_name=[];
$('#Button1').click(function(){
var textbox_value=$('#Text1').val();
array_name.push(textbox_value);

for(var i=0;i<array_name.length;i++)
alert(array_name[i])
});
})
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter Your name:
<input id="Text1" type="text" value="Jacob" /><br />
<input id="Button1" type="button" value="button" />
    </div>
    </form>
</body>
</html>
When we click on button run JQuery function and insert item in the array.

Friday, June 12, 2015

How to convert decimal number to round off figure using JQuery

If you want to display decimal number to fix digit number. I mean to say i have a decimal number like 123.123 and i want to display round of number then use Jquery round method.


 <%@ Page Language="C#" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="Scripts/jquery-1.10.2.js"></script>
<script>
$(function(){
$('#Button1').click(function(){
$('#result').html(Math.round($('#Text1').val()))
});
})
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter Number: <input id="Text1" type="text" value="123.123" />
<input id="Button1" type="button" value="Round Off value" />
<br />
<label id="result" style="font-size:16px" />
    </div>
    </form>
</body>
</html>
When we click on the button , TextBox value will be converted into Round Off value using JQuery method.

© Copyright 2013 Computer Programming | All Right Reserved