Breaking News

Editors Picks

Tuesday, July 3, 2012

parameter in sqldatasource with select command

SqlDataSource allows you to Select  the data from the database without even writing a single line of code. But sometimes you need to dynamically assign a parameter. First let's take a look at the  dynamically assign the parameters.


<%@ 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>SqlDatasource select parameterstitle>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 157px;
        }
    style>
head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td class="style2">
                    User Name
                td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
                td>
            tr>
            <tr>
                <td class="style2">
                    Password
                td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server">asp:TextBox>
                td>
            tr>
            <tr>
                <td class="style2">
                     
                td>
                <td>
                    <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
                td>
            tr>
            <tr>
                <td colspan="2">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="Horizontal" AllowPaging="True" AllowSorting="True"
                        DataSourceID="SqlDataSource1" Width="469px">
                        <RowStyle BackColor="#EFF3FB" />
                        <Columns>
                            <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
                            <asp:BoundField DataField="cityname" HeaderText="cityname" SortExpression="cityname" />
                        Columns>
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />
                    asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT username,cityname FROM useraccessprofile WHERE UserName = @UserName AND pwd = @pwd ">
                        <SelectParameters>
                            <asp:ControlParameter Name="UserName" ControlID="TextBox1" PropertyName="Text" />
                            <asp:ControlParameter Name="pwd" ControlID="TextBox2" PropertyName="Text" />
                        SelectParameters>
                    asp:SqlDataSource>
                td>
            tr>
            <tr>
                <td class="style2">
                     
                td>
                <td>
                     
                td>
            tr>
        table>
    div>
    form>
body>
html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

No comments :

Post a Comment

Contact Us

Name

Email *

Message *