-->

Tuesday, September 10, 2013

How to Insert record using InsertParameters in ASP.NET

DotNet provides easy methods for inserting values to database tables or you can say that dotnet reduce lots of code for binding.
There are many properties exist in DOTNET library for inserting records into the database table such as Control , Cookies , Form , Profile, Querystring , Route and session.  Here we are going to insert record into database table through dotnet controls .If you want to add record from the control using GridView InsertParameters then you can follow some steps :

Step-1 : Add control to the Design page.

How to Insert record using InsertParameters in ASP.NET

  Step-2: Add one GridView control to the Design page.
Step-3: Choose DataSource from the showsmart tag.

How to Insert record using InsertParameters in ASP.NET
Step-4: Select DataSource as  SQL in Configuration wizard

How to Insert record using InsertParameters in ASP.NET

Step-5: If your database exist in your solution explorer then you can select database from dropdown otherwise make a new connectionString using New Connection button

How to Insert record using InsertParameters in ASP.NET

Step-6 : Select II radio button (specify columns from a table view) in given configure DataSource. Also click Advance button for selecting special features.

How to Insert record using InsertParameters in ASP.NET
Step-7: If your table having one primary key then your CheckBox are enabled otherwise your checkbox will disabled so specify one primary key column in your table column for using special feature.


How to Insert record using InsertParameters in ASP.NET

Step-8: Test Your Query by pressing Test Query Button. Also click to Finish button.

How to Insert record using InsertParameters in ASP.NET

Step-9 : Open your page in source mode. Now your code look like

How to Insert record using InsertParameters in ASP.NET

Step-10 : Edit your InsertParameters. Replace asp:Parameter to asp:ControlParameter also add ControlID attribute in  asp:ControlParameter tag. Now my code is 

   <InsertParameters>
                <asp:ControlParameter Name="namet" Type="String" ControlID ="TextBox1" />
                <asp:ControlParameter Name="Income" Type="Int32" ControlID ="TextBox2" />
            </InsertParameters>


Step-11 : Add one Insert() method of the SqlDataSource class on button click event

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

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Insert();
    }
</script>

<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"></asp:TextBox><br />
        Enter Income:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Width="107px" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
        <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:ControlParameter Name="namet" Type="String" ControlID ="TextBox1" />
                <asp:ControlParameter Name="Income" Type="Int32" ControlID ="TextBox2" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="namet" Type="String" />
                <asp:Parameter Name="Income" Type="Int32" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Output
How to Insert record using InsertParameters in ASP.NET

Monday, September 9, 2013

Secure Password with Password Box Control: WPF

Textbox is used to input simple string type values and some alphanumeric values. What is inserted in the textbox, can be read by the user easily. But when we are creating the login form or any form for security purposes, then we have to hide the text/value written by the user.

For these purposes textbox doesn’t have a password mode in WPF, so we have to use password box in place of textbox. Password box is designed to enter password by the user because, the colleague can’t read the inputted value by us. It doesn’t allow editing options like cut, copy and paste etc.
<StackPanel>
<Label Content="Password" Width="200" Height="25"></Label>
<PasswordBox Width="200" Height="25"/>
</StackPanel>

By using the above code, a password box has been created and when we will write something into this, it will be converted into bullets. The bullets are the standard password character format of windows now like in windows 8.

Secure password using password box in WPF

We can change some of the properties of this password box, which are mostly used in WPF:
  • PasswordChar: used to change the password character. <PasswordBox Width="200" Height="25" PasswordChar="*"/>
  • MaxLength: limit the length of the password. <PasswordBox Width="200" Height="25" MaxLength=”6”/>

How to Add SubMenus in Menu control: WPF

What will be the use of Menu control if we add only one line menus like in our previous post? Menu control is used to organize elements in hierarchy view. In previous post we have added some menu items, in this post we will add some child elements in those elements.

Just use the below lines of code in XAML to generate a menu control with some child elements:
<Menu IsMainMenu="True">
<MenuItem Header="_First">
<MenuItem Header="Child 1"></MenuItem>
<MenuItem Header="Child 2"></MenuItem>
<MenuItem Header="Child 3"></MenuItem>
</MenuItem>
<MenuItem Header="_Second">
<MenuItem Header="Child 4"></MenuItem>
<MenuItem Header="Child 5"></MenuItem>
<MenuItem Header="Child 6"></MenuItem>
</MenuItem>
<MenuItem Header="_Third">
<MenuItem Header="Child 7"></MenuItem>
<MenuItem Header="Child 8"></MenuItem>
</MenuItem>
</Menu>

It will add three children to first element, three to second element and two to third element. The image shows the first three:

How to add Submenu and expand them WPF


Menu control provides a checkbox feature with each of the item, that can be enable using IsCheckable property to true. After enable the items cab be checked individually like:

How to create a checkable menus in WPF

AccessKey Property of TextBox in ASP.NET

In previous article , we have already learned about TextBox Control. Using the AccessKey property you can directly navigate to the control Or you can say directly focus to the control. Accesskey property is basically used for when you have a large page content and you want to directly navigate to particular control on that time you should use AccessKey property. Another one example is , suppose your mouse are not working properly on that time AccessKey will working for focusing control.
AccessKey Property of TextBox in ASP.NET
source : franz.com
In Above mentioned diagram suppose you have not mouse Then , i am asking you that How to do check, given CheckBoxes. Lastly i am telling you that if you want make user friendly software then you should use AccessKey property with some value. Bydefault AccessKey is set to null no more value assign to Accesskey that means not set.  

Lets take a simple example


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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <span class="auto-style1">U</span>serName :
        <asp:TextBox ID="TextBox1" runat="server" AccessKey="U" Width="159px"></asp:TextBox>
        <br />
        <span class="auto-style1">P</span>assWord :&nbsp;
        <asp:TextBox ID="TextBox2" runat="server" AccessKey="P" Width="157px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />
 
    </div>
    </form>
</body>
</html>

Output
AccessKey Property of TextBox in ASP.NET

AccessKey Property of TextBox in ASP.NET

Sunday, September 8, 2013

How to Add Menu Control: WPF

Any GUI window in our computer like Notepad, MS Word, browsers or even visual studio have its own menu bar. As compare to other controls Menu bar helps user to do the desired task in simple & easy way. Menu bar can be added as the Menu control in programming languages.



WPF menu control enables the user to organize elements in hierarchy view. To add a menu bar, you have to just drag-n-drop the Menu control from the toolbox or write following code in XAML:

<Menu/>

It may be our main menu with IsMainMenu property to true. Writing above line of code will show nothing in the window, till we don’t add any item in it. Item can be added using MenuItem class with Header property.

In the following lines of XAML code, I have add three items in the menu bar.
<Menu IsMainMenu="True">
<MenuItem Header="First"></MenuItem>
<MenuItem Header="Second"></MenuItem>
<MenuItem Header="Third"></MenuItem>
</Menu>

Just run the code and it will show a window containing a menu bar with three items:

Menu bar control in WPF

Now as in other windows, user presses the left Alt key and menu bar has been highlighted, keyboard shortcut cab also be used by using underscore before the character as in following code.
<Menu IsMainMenu="True">
<MenuItem Header="_First"></MenuItem>
<MenuItem Header="_Second"></MenuItem>
<MenuItem Header="_Third"></MenuItem>
</Menu>

Now it will be used by keyboard shortcut as in other windows. Run the project and press left alt key it will highlight the menu bar as in following image:

Highlighted menu bar control WPF

How to get root directory information of the specified path in ASP.NET

Path class

Suppose you have a specified path such as "C:/my files/abc.txt" and you want to access root of the specified path (Root is "C:/"). If your path does not contain root directory information then your path class method returns null value or empty string. For accessing root information use GetPathRoot( ) method of the  Path class which is stored in System.IO namespace.

 Lets take an example 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text;
            string getroot = Path.GetPathRoot(path);
            label2.Text = "Your root directory is "+getroot;


        }
    }
}

.    Output
How to get root directory information of the specified path in ASP.NETHow to get root directory information of the specified path in ASP.NET


How to Bind ListView with string List: WPF

As we all know the listview is inherited class of listbox and have almost all the properties of listbox. We have learnt listbox binding with string list in our earlier post. In this article we bind the listview with the same string list.

Just place a listview having only name property, because we have to access that listview in our code behind file. We can also use height and width property of the listview, if the data items are of lengthy string.
<ListView>
<ListView.Name>listView</ListView.Name>
</ListView>

Now use the same list of string as in previous post i.e.
List<string> strList = new List<string>();
strList.Add("London");
strList.Add("Italy");
strList.Add("California");
strList.Add("France");
listView.ItemsSource = strList;

Now look out the last statement, it will set the item source of the listview as this string list. When we run the project it will show a listview with four items:

Bind listview with string list in WPF C#


To get selected item the same procedure will follow as in listview bind with grid resource.
© Copyright 2013 Computer Programming | All Right Reserved