-->

Thursday, September 19, 2013

How to use AddWithValue Method in ASP.NET

Adds a value to the end of the System.Data.SqlClient.SqlParameterCollection. Syntax of the method is

variable of SqlCommand.Parameters .AddWithValue (String parameterName , Object value);
using Parameters collection you can insert values to the database.

Lets take an example how to insert item into database table.




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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        Enter name :
        <asp:TextBox ID="TextBox1" runat="server" Width="183px"></asp:TextBox>
        <br />
        Enter Address :
        <asp:TextBox ID="TextBox2" runat="server" Width="183px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
            Text="Submit" />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
    </div>

    </form>
</body>
</html>
Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    protected void Button1_Click1(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
        {
            con.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@nameprameter", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Addressparameter", TextBox2.Text);
            cmd.CommandText = "insert into [mytab](name,address)values(@nameprameter,@Addressparameter)";
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                Label1.Text = "insert data";
            }


        }

    }
}
Output
How to use AddWithValue Method in ASP.NET

How to use DataRow class in ASP.NET

Lets take an example to bind DropDownList using DataRow class.

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:Button ID="Button1" runat="server" Height="37px" onclick="Button1_Click"
            Text="Bind DropDownList" Width="130px" />
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </div>

    </form>
</body>
</html>

Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [mytab]", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            
                        da.Fill(dt);
                        if (DropDownList1.Items.Capacity == 0)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                string name = dr["name"].ToString();


                                DropDownList1.Items.Add(name);


                            }
                        }
     
        }
    }
}

Output
How to use DataRow class in ASP.NET

Wednesday, September 18, 2013

How to use DataTable in ASP.NET


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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:Button ID="Button1" runat="server" Height="37px" onclick="Button1_Click"
            Text="Bind Gridview" Width="130px" />
        <br />
  
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>
Codebehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [mytab]", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
                       GridView1.DataSource = dt;
            GridView1.DataBind();
          

        }
    }
}


Output
How to use DataTable in ASP.NET

How to Place Items in TabControl: WPF

In our previous post, we have learnt about tab control and place some tabs into that control. In this post we will place a container, means we will place a simple entry form into a tab item. As we all know the entry form can contain name, age, address or some more basic information about a person.

So I am taking only name and age here as for example. Just write the following code in our XAML code window:
<TabControl Name="tabControl" Margin="5">
<TabItem Header="Entry Form Tab">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Label Content="Name"/>
<Label Content="Age" Grid.Row="1"/>

<TextBox Margin="2" Grid.Column="1"></TextBox>
<TextBox Margin="2" Grid.Column="1" Grid.Row="1"></TextBox>
</Grid>
</TabItem>
</TabControl>


Just run the code and a tab control is placed on our window with an entry form. The form will look like the following image:

How to customize tab control in WPF and XAML

So we have simply placed an entry form into tab item. We can set anything as the content property of tab item, just as above code.

In the above code, i have modify only a single tab item for example. If you need more tabs then you can modify as many tabs as you want to.

How to Create Tabs using TabControl: WPF

When there is not much space on our screen then programmer uses a tab control. Tab control is used to organize multiple tabs on WPF window, which can performs individual function decided by the programmer. It can contains collection of objects of any type i.e. string, image or a container.

Through XAML, we can place a tab control easily using the element Tab Control. To place some tabs into tab control, we have to use TabItem elements. Write the following code in your XAML:
<TabControl Name="tabControl" Height="400" Width="300" Margin="5">
<TabItem Header="Tab 1" Width="70"></TabItem>
<TabItem Header="Tab 2" Width="70"></TabItem>
<TabItem Header="Tab 3" Width="70"></TabItem>
</TabControl>

Now run the code and check out the tab control. It have three tabs with their own header as described above.

How to add a Tabcontrol in XAML and WPF


The same tab control can also be generated through the following code in C# code behind file:
TabControl tabcontrol = new TabControl();
tabcontrol.Name = "tabControl";
tabcontrol.Height = 400;
tabcontrol.Width = 300;
tabcontrol.Margin = new Thickness(5);

TabItem tabItem1 = new TabItem();
tabItem1.Header = "Tab 1";
tabcontrol.Items.Add(tabItem1);

TabItem tabItem2 = new TabItem();
tabItem2.Header = "Tab 2";
tabcontrol.Items.Add(tabItem2);

TabItem tabItem3 = new TabItem();
tabItem3.Header = "Tab 2";
tabcontrol.Items.Add(tabItem3);

this.Content = tabcontrol;

In next article, we will place some items/containers in these tab items. The containers could have other items/containers.

How to bind CheckBoxList using SqlDataSource in ASP.NET

SqlDataSource 

The SqlDataSource control is a DataSource control that allows a server control, Such as the GridView control, to access Data located in a relational database, such as Microsoft SQL Server and Oracle.
This control also generates a connection string to interact with data in an ASP.NET page. These connection strings are generated through the Configure Data Source wizard. A Connection String contains informattion about the server, database , and security you are working with in your application , for example
Data Source= .\SqlExpress; Initial Catalog =Database name ; Integrated Security = True
The SqlDataSource control automatically opens a database connection , which is provided in the connection string, executes the SQL queries , and then close the connection. When you add a SqlDataSource control to an ASP.NET page , the following code appears in the HTML code of the page.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"> </asp:SqlDataSource>

CheckBoxList 

You can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes . The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a data source to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these control to a data source.

Lets take an example for binding CheckBoxList using SqlDataSource
Step-1: Drop CheckBoxList control to design window 
Step-2: Choose DataSource from showSmart tag ..... 
Step-3:  Same as GridView Binding

Complete Source code




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

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

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
        </asp:CheckBoxList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [mytab]"></asp:SqlDataSource>
  
    </div>
    </form>
</body>
</html>

Output
How to bind CheckBoxList using SqlDataSource in ASP.NET

Tuesday, September 17, 2013

How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

If you bind your application with database in traditional ASP program then you should use RecordSet and write lots of code for binding . But in ASP.NET Microsoft reduce lots of code for binding application. In ASP.NET you can use SqlDataSource for connecting front-end to back-end.

lets take an example , Add insert , update and delete functionality through SqlDataSource.
Step-1: Drop SqlDataSource control to the design window.
Step-2: Select Configure Data Source using Show Smart tag.
Step-3: Select Connection from DropdownList , expand connection string
now your connection string are
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
Click Next to continue
Step-4: Select Advanced.. Button for adding insert,edit and delete functionality.
How to Insert,Edit,Delete Record using SqlDataSource
If your database table contain one primary key then you can select "Generate INSERT, UPDATE, AND DELETE STATEMENT"  And click to Ok button.

How to Insert,Edit,Delete Record using SqlDataSource
Step-5: After finishing process , Drop one DetailsView Control to the page. and Choose SqlDataSource1 for connecting database using ShowSmart tag.
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET
Step-6: Select all given CheckBox
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [footerex] WHERE [Id] = @Id" InsertCommand="INSERT INTO [footerex] ([namet], [Income]) VALUES (@namet, @Income)" SelectCommand="SELECT * FROM [footerex]" UpdateCommand="UPDATE [footerex] SET [namet] = @namet, [Income] = @Income WHERE [Id] = @Id">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="namet" Type="String" />
                <asp:Parameter Name="Income" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="namet" Type="String" />
                <asp:Parameter Name="Income" Type="Int32" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
 
    </div>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="namet" HeaderText="namet" SortExpression="namet" />
                <asp:BoundField DataField="Income" HeaderText="Income" SortExpression="Income" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </form>
</body>
</html>


Output
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved