Saturday 22 June 2013

Databinding With ComboBox in ADO.NET

Databinding With ComboBox in ADO.NET




Introduction
In this article I am describing various techniques of databinding with a ComboBox control. A ComboBox control shows data in a dropdown list. Before starting the databinding with ComboBox, let's know its two important properties -  DisplayMember and ValueMember.

DisplayMember : This property is used to specify the name of a column ( Database Table's Column ) to be displayed in the ComboBox.

ValueMember : This property is used to  specify the name of a column ( Database Table's Column ) to be stored as values of selected item in ComboBox. 
I am using a "student" database which has a table "student_detail" with some records. Now take a Windows Forms application -> Take a label and ComboBox controls as in the following figure.



Go to smart property of ComboBox control.

Follow the given steps.
Step 1 : Check the CheckBox of Use Data Bound Items.

Step 2: Click on the down arrow of the Data Source.
Step 3: Click at Add Project Data Source link. A new window will open.

Step 4: Select "Database" as Data Source ( By default Database is selected ). Click the next button. A new window will open.
Step 5: Select "Dataset" as Database model ( By default Dataset is selected ). Click the next button. A new window will open.
Step 6: Click the "New Connection" button. A new window will open. In the new window enter the Server name (I have used dot), write user name and password of your SQL Server and select database ( I have selected "student" ). Look at the following figure.
Step 7: Click the "ok" button. Now a connection has been made to the student database. You will look at the "Data Source Configuration Wizard" window. 



Step 8: Click the next button.
Step 9: Click the plus ( + ) sign to explore all tables of the student Database.

Step 10: Again click the plus sign for "student_detail" to show all columns of the Database table. Check the all CheckBox.

Step 11: Click the Finish button. Now set the DisplayMember and ValueMember property of ComboBox. Click at down arrow of Display Member and select "rollno". Same as set "name" as Value Member property.


Run the application.

Output


It will show all values from the "RollNo" column because we have set RollNo to the DisplayMember property of combobox. Now stop the running program -> go to design window of your application and write the following code on SelectedIndexChanged event of ComboBox.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
      label1.Text = comboBox1.SelectedValue.ToString();
 }

Run the application. Select a different value in ComboBox. It will show the student name in Label regarding to selected Roll Number.



Now we bind the ComboBox with data by code. Take another ComboBox ( Say CombBox2) and a Label and write the following code.
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
 DataBindingWIthComBox
{
    public partial class Form1 : Form    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlDataAdapter dadpter=new SqlDataAdapter("select * from student_detail","server=.;database=student;user=sa;password=wintellect");
            DataSet dset = new DataSet();
            dadpter.Fill(dset);
            comboBox2.DataSource = dset.Tables[0];
            comboBox2.DisplayMember = "rollno"// to display roll no. in combobox            comboBox2.ValueMember = "name"// to store name as value of combobox for selected roll no.         }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            label2.Text = comboBox2.SelectedValue.ToString();
        }
    }
}
 
Run the application. It will do same work as before.

Friday 21 June 2013

ASP.NET

ASP.NET

ASP.NET is a new ASP generation. It is not compatible with Classic ASP, but ASP.NET may include Classic ASP.
ASP.NET pages are compiled, which makes them faster than Classic ASP.
ASP.NET has better language support, a large set of user controls, XML-based components, and integrated user authentication.
ASP.NET pages have the extension .aspx, and are normally written in VB (Visual Basic) or C# (C sharp).
User controls in ASP.NET can be written in different languages, including C++ and Java.
When a browser requests an ASP.NET file, the ASP.NET engine reads the file, compiles and executes the scripts in the file, and returns the result to the browser as plain HTML.

Binding GridView with Data - The ADO.NET way

GridView

This example demonstrates how to populate GridView with data from the database using the ADO.NET way. 

Before you proceed reading this example be sure that you know the basics of ADO.NET manipulation. If you are not familiar with ADO.NET then I would suggest you to refer at the following link below:


STEP 1: Setting up the Connection String 

- Open your Web.config file and set up your connection string like below:
<connectionStrings>
    <add name="MyDBConnection" connectionString="Data Source=WPHVD185022-9O0;
                               Initial Catalog=Northwind;
                               Integrated Security=SSPI;"
                               providerName="System.Data.SqlClient"/>
</connectionStrings>


STEP 2: Create the GetConnectionString() method

- Create a method for accessing your connection string that was set up at the Web.config file
private string GetConnectionString(){
 return ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString;
}
Note: MyDBConnection is the name of the connectionstring that was set up in the webconfig. 

STEP 3: Setting up the GridView in the mark up (ASPX) 

- Grab a GridView from the Visual Studio ToolBox and then Set AutoGenerateColumns to False. 
- Add BoundField Columns in GridView and set the DataField and the HeaderText accordingly. See below:
<!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>Populating GrindView Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="CompanyName"  HeaderText="Company"/>
            <asp:BoundField DataField="ContactName" HeaderText="Name"/>
            <asp:BoundField DataField="ContactTitle" HeaderText="Title"/>
            <asp:BoundField DataField="Address" HeaderText="Address"/>
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

STEP 4: Create the BindGridView() method 

- After setting up your GridView in the mark up then switch back to Code behind 
- Declare the following NameSpace below so that we can use the SqlClient built-in libraries 
using System.Data.SqlClient;

-Create the method for Binding the GridView
private void BindGridView()
{ 

        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(GetConnectionString()); 

        try
        {
          connection.Open();
          string sqlStatement = "SELECT * FROM Customers";
          SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
          SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
          sqlDa.Fill(dt);

              if (dt.Rows.Count > 0)
              {
                GridView1.DataSource = dt;
                GridView1.DataBind();
              }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
}

STEP 5: Calling the BindGridView() method on initial load of the page.
protected void Page_Load(object sender, EventArgs e){
        if (!Page.IsPostBack){
            BindGridView();
        }
}


STEP 6: Displaying the Output in the page
 

- Compile and run your page to see the output.. see below 

 


That simple! 

Monday 17 June 2013

Open Miracle



Open Miracle 
www.openmiracle.com
                                                                   Open Miracle  is an open source accounting based software with lots of automated features designed to take the load off small as well as large business owners.
There is enough accounting software out there for megacorps,but Open Miracle  is all  about making things easy for small as well as large business owners,even if they have no accounting knowledge.
       Open Miracle  accounting software can easily create professional-looking, personalized invoices to help you get paid quickly and track payment information for tax purposes.
Open Miracle  is efficient, easy-to-use accounting software bundled with project tracking, task management, collaboration and payment processing.
download at www.openmiracle.com

M I R A C L E  -Manage.Inventory.Revenues.Assets.Capital.Liabilities.Expenses 

download at www.openmiracle.com

 
- Joseph