-->

Thursday, October 10, 2013

How to Load UserControl in ToolBar Control: WPF

In earlier post I have placed some images and labels in toolbar control using some lines of XAML code. Now if we want to load a user control, written in another file, we have so follow some steps like the below.

Just add a user control through “Add New Item” from the file menu or from the right click on the project. Rename it if you want and write the following XAML code to add two labels and two textbox in this user control:
<StackPanel Orientation="Horizontal">
<Label Content="Name"></Label>
<TextBox Width="50"></TextBox>
<Label Content="Age"></Label>
<TextBox Width="40"></TextBox>
</StackPanel>

Now open our WPF window and add a reference of our current project (or the project which contains our user control), just like using:

xmlns:uc="clr-namespace:WpfApplication1"

Now write the following line of XAML code in the main content of the window:
<ToolBar Name="toolbarControl">
<uc:UserControl1></uc:UserControl1>
</ToolBar>

Run the project and the toolbar control will be load with the above user control.

How to load user control in toolbar control: WPF XAML

How to Place an Image in Toolbar Control: WPF

Toolbar control is used to place some shortcuts in the windows environment. We can use this toolbar in our WPF application as used in our previous post. In this post, we will place an image control in to this toolbar.

We can directly place the image or we can use a stack panel to use an image. Just write the following XAML code:

<ToolBar VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<Label Content="Image 1"></Label>
<Image Source="buttonImage.png" Width="80" Height="40"></Image>

<Label Content="Image 2"></Label>
<Image Source="image1.png" Width="80" Height="40"></Image>
</StackPanel>
</ToolBar>

Run the above code and it will place two label control with two images respectively. These two images are placed in my solution files. The images are fix proportionally according to the width and height provided.

How to place an image control in ToolBar Control: WPF XAML


The same process may be done by the c# code (through code behind file). We can use as many images as we want to use in a single toolbar control. In the next article we will load a user control in this toolbar control.

See Also: How to Load UserControl in Toolbar Control

Difference between static and dynamic websites

Website: a website is nothing but a place which delivers information to many clients which connected with it. This place is called web server indicate a single domain where the data actually located. Generally this is called URL (Universal resource locator) of that web server. User sends a request to access data item through the web protocol i.e. HTTP. The web server gives the response to the client, this response contain the data in form of markup. This response is called web page. This webpage created from any markup language like HTML, which holds the contents like text, images, animation, links , audio, video etc. so one line definition a website as " A website is collection of Webpages which deliver information to the client from the web server over the network".


Download C++ Videos



Difference between static and dynamic websites


In real time system we have two types of websites.

1. Static websites: - static websites contain fixed number of pages and format of web page is fixed which delivers information to the client. There is 110 change in contents of web page while page is running on client's browser. This kind of web sites created from HTML and CSS coding on simple text editor like notepad. Example an organization site, institute site etc.

2. Dynamic websites: - dynamic websites can change the web page contents dynamically while the page is running on client's browser. This kind of websites use server- side programming like PHP, Asp.NET. and JSP etc. to modify page contents on run time. Dynamic websites use client side scripting for prepare dynamic design and server- side code to handle event, manage session and cookies, and storing and retrieving data from database. Example E-commerce sites, online form application, E-governance site, social networking sites etc.

Static websites
Dynamic websites
Static websites contain fixed number of pages.
Dynamic websites can create webpage dynamically.
Theme of website and content of webpage are fixed.
Webpage design and content may change on run time.
Static websites load quickly on client browser because it has only some markup contents.
Dynamic sites take some time to load on client browser because it processes the request server side and create contents dynamically.
Static sites never use database connectivity.
Dynamic sites deal with database and generate the contents dynamically using database queries.
Static websites is highly secure than dynamic sites because it behaves as a half duplex approach so only one way communication is possible i.e. server to client.
Dynamic sites are less secure because it behaves as full duplex approach so both side communications is possible so user can change the server data.

Static site use for provide some information to the clients like an organization or institute website.
Dynamic website use where content changes frequently on run time. Like a E-commerce site, online examination, etc.
Static website directly run on browser and does not require other server application language. Static website can be created from HTML and CSS.
Dynamic website run the application on server and the output will display on webpage. So this is require server application language like PHP , Asp.NET, JSP etc.
Static sites are easy to develop and a bit experienced people can develop it.
Dynamic websites not easy to develop because require qualify developers to create it, manage it, test it and maintain security of application and database.
In static website if we want to change the page content then we have to upload that page on server many times.
Dynamic sites provide the facilities that it possible to change the page content using server application. And need not to upload the page on server.

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below:
ASP.NET Video Tutorial Series

Structure

Union

1.The keyword  struct is used to define a structure
1. The keyword union is used to define a union.
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of  sizes of its members. The smaller members may end with unused slack bytes.
2. When a variable is associated with a union, the  compiler allocates the  memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
3. Each member within a structure is assigned unique storage area of location.
3. Memory allocated is shared by individual members of union.
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
5 Altering the value of a member will not affect other members of the structure.
5. Altering the value of any of the member will alter other member values.
6. Individual member can be accessed at a time
6. Only one member can be accessed at a time.
7. Several members of a structure can initialize at once.
7. Only the first member of a union can be initialized.

How to insert multiple records into database with SqlParameter in C#

Introduction 

Represents a parameter to a SqlCommand and optionally its mapping to DataSet. Simply you can say, you can add parameter to SqlParameter constructor. Like

new SqlParameter("variable name","value");

Pass this parameter to SqlCommand parameter.

SqlCommand cmd = new SqlCommand();  Like that 

Cmd . Parameter.Add(new SqlParameter("variable name","value")) ;

 Also you can represents some properties of the SqlParameter to SqlCommand something like that.

cmd.Parameters["variable name"].Value = value;
cmd.Parameters["variable name"].Size = 255;
cmd.Parameters["variable name"].SqlDbType = SqlDbType.NVarChar;

Lets take a Simple Example to Insert Multiple record using SqlParameter.

Source code


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

<!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>
    Name :&nbsp;&nbsp;
        <asp:TextBox ID="nmetxt" runat="server"></asp:TextBox> <br />

    Address:  <asp:TextBox ID="addtxt" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

</html>
Codebehind File


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 _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand();

        cmd .Parameters .Add(new SqlParameter("@name11",nmetxt .Text ));
        cmd.Parameters .Add (new  SqlParameter ("@Add",addtxt .Text ));
        cmd.Parameters["@name11"].Value = nmetxt.Text;
        cmd.Parameters["@name11"].Size = 255;
        cmd.Parameters["@name11"].SqlDbType = SqlDbType.NVarChar;

        cmd.Parameters["@Add"].Value = addtxt.Text;
        cmd.Parameters["@Add"].Size = 255;
        cmd.Parameters["@Add"].SqlDbType = SqlDbType.NVarChar;


        cmd.Connection = con;
        cmd.CommandText = "Insert into [dbtable](name1,Address)values(@name11,@Add)";
        cmd.CommandType = CommandType.Text;

        con.Open();
        int a=cmd.ExecuteNonQuery();
        if (a>0)
        {
            Label1.Text = "sucess";
            Label1.BackColor = System.Drawing.Color.Green;
            Label1.ForeColor = System.Drawing.Color.White;
        }
        con.Dispose();


    }
}


Output
How to insert multiple records into database with SqlParameter in C#

How to insert multiple records into database with SqlParameter in C#

How to pass list of string from Controller to View in MVC using ViewData

Introduction 

ViewData is used to pass Data from controller to View in MVC. ViewData is a Dictionary of Objects that are stored and retrieved using string as a Keys such as 

ViewData["Key"] = Value; // Stored value in ViewData.

String n = ViewData["Keys"] ;  // retrieve from DataView


Controller class code (HomeController.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
           ViewData["countries"] = new List<string>()
            {
                "USA",
                "INDIA",
                "UK"
            };
            return View();
        }

    }

}
Index.cshtml class code in View
@{
    ViewBag.Title = "List of string value";
}


<h2>Country List</h2>
<ul>
    @foreach (string  country in (List<string>)ViewData["countries"])
    {
        <li>@country</li>
    }


</ul>
Output
How to pass list of string from Controller to View in MVC using ViewData

Wednesday, October 9, 2013

Difference/Similarities between ViewBag and ViewData in MVC

Differences
ViewData
ViewBag
ViewData is a dictionary of objects that are stored and retrieved using strings as keys.
Such as:

ViewData[“key”]=value; // stored value in ViewData

String n = ViewData[“Key”]; // Retrieve Data





ViewBag uses the dynamic feature that was introduced in to c# 4. It allows an object to have properties dynamically add to it.

ViewBag.name = “Jacob”;

// here name is the dynamic property of //ViewBag object

Similarities
·         Both ViewData ans ViewBag are used to pass data from a controller to a View.
·         Both ViewData & ViewBag does not provide compile time error checking

Example
Controller class
  public ActionResult Index()
        {
            ViewBag.country = new List<string>()
            {
                "USA",
                "INDIA",
                "UK"
            };
            return View();
        }

View
<ul>
    @foreach (string  country in ViewBag .coun)
    {
        <li>@country</li>
    }



</ul>

·         Note  : In View here you are accessing dynamic property of coun , which is not existing in Controller class. But in case Both ViewData & ViewBag does not provide compile time error checking




© Copyright 2013 Computer Programming | All Right Reserved