Click here to Skip to main content
15,887,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have serch button .
i hve connected formview with sqldatasource
by query
select * from tbregis where username=@username
on page i have a button and textbox

i want that if i write on textbox some username than formview shud be visible showing the username attributes
other wise it shud leave a message record not found!
how can i do that any solution
Posted

You can use FormView.EmptyDataTemplate property.

I hope it is useful.





---------------------------------
My site: Imobiliárias em Guarulhos
 
Share this answer
 
Default3.aspx


XML
<form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" Text="Please Enter your name here :"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" Height="26px"
        ontextchanged="TextBox1_TextChanged" style="margin-left: 13px" Width="161px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        style="margin-left: 13px" Text="Click Me" Width="108px" />
</div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
    DataSourceID="SqlDataSource1">
    <EditItemTemplate>
        ID:
        <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
        <br />
        Name:
        <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
            CommandName="Update" Text="Update" />
        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <InsertItemTemplate>
        ID:
        <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
        <br />
        Name:
        <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
            CommandName="Insert" Text="Insert" />
        &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </InsertItemTemplate>
    <ItemTemplate>
        ID:
        <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
        <br />
        Name:
        <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
        <br />
    </ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CPTempDBConnectionString %>"
    SelectCommand="SELECT * FROM [Student] WHERE ([Name] = @Name)">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" Name="Name" PropertyName="Text"
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="Label2" runat="server" Text="Record Not Found"></asp:Label>
</form>


Default3.aspx.cs

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        // Same thing can be done on button click event also
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (TextBox1.Text != String.Empty)
        {
            FormView1.DataBind();
            if (FormView1.DataItemCount > 0 ) Label2.Visible = false;
            else Label2.Visible = true;
        }
        else
        {
            Label2.Visible = true;
        }
    }
 
Share this answer
 
v2
Comments
sandipapatel 5-Dec-10 7:48am    
DO NOT FORGET TO VOTE ME IF IT WORKS FOR YOU.
Student Database has two fields : ID , Name
By entering correct Name it will show FormView with details and if incorrect name or emptry name then Shows "Record Not Found" as per u r requirement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900