Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello experts,

am working with search in asp.net C#, it is working fine.
but am facing problem with other records which are not existing.

if the user enter other records in Search Textbox,, which are not exisitng in database.
it should display a popup message as (Sorry Record not found).

This is my Search button code:
C#
protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
    {
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GVRecords.DataSource = ds;
        GVRecords.DataBind();
        con.Close();
}

Please help me..

thanks.
Posted
Updated 12-May-13 9:02am
v2
Comments
Sandeep Mewara 12-May-13 15:02pm    
Why popup? Thats irritating! Why not simple label text?
Ranjith Reddy CSE 13-May-13 1:47am    
or else Please give me solution using LABEL. for No Records found.

Already shared as a comment: Why popup? Thats irritating! Why not simple label text?

If you still want to show popup, you can either get the count using XMLHttpRequest and show message if count is zero.
Or Inject JavaScript code from server side using:
MSDN: RegisterClientScriptBlock Method[^]
MSDN: RegisterStartupScript Method[^]

Try something like:
C#
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('No records found')", true);
 
Share this answer
 
Comments
Maciej Los 12-May-13 17:42pm    
+5
Ranjith Reddy CSE 13-May-13 1:48am    
or else Please give me solution using LABEL. for No Records found.
Sandeep Mewara 13-May-13 2:18am    
Well for label, all you need is to get the result, see count, if zero, set the label text as 'No record found' and set the visible property of it to true (already placed label in location needed set as visible false). As simple as that!
Hi,

I would suggest to show a label just above the data grid displaying the number of records searched. Popups can be used while validating the input for search or submit forms.
But if at all you want to display a popup if there are no records, you can use AjaxControlToolkit.

Download the tool kit from http://ajaxcontroltoolkit.codeplex.com/[^]

Add a reference of the tool kit dll to your ASP.Net project and to your page as (just below the @Page attribute)
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyTIO.Master" AutoEventWireup="true" CodeBehind="example.aspx.cs" Inherits="example.example.example" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


Add an asp:Panel (not displayed initially) for the message as below:

ASP.NET
<asp:panel runat="server" id="pnlMessage" style="display:none" xmlns:asp="#unknown">
</asp:panel>


Format the panel in the CSS as you want.

Then, just below the search button, add a dummy button as follows:

<asp:button runat="server" id="btnSearchRecords" text="Search Records" onclick="btnSearchRecords_Click" xmlns:asp="#unknown" />
<asp:button runat="server" id="btnDummySearchRecords" style="display:none;" xmlns:asp="#unknown" />


Then add the ajax control tool kit modal popup extender as follows:

XML
<asp:Button id="btnSearchRecords" Text="Search Records" onClick="btnSearchRecords_Click" />
<asp:Button id="btnDummySearchRecords" style="display:none;" />
<cc1:modalpopupextender id="mpeMessage" targetcontrolid="btnDummySearchRecords" backgroundcssclass="MessageBackground" xmlns:cc1="#unknown">
                    PopupControlID="pnlMessage"
                    OkControlID="btnClose" 
                    DropShadow="true
</cc1:modalpopupextender>


In the button click event in c#, the code looks like as follows:

C#
protected void btnSearchRecords_Click(object sender, EventArgs e)
{
    //Code to query the database and get the results in the datatable
    DataTable searchedRecords = new DataTable();
    searchedRecords = QueryDatabaseAndGetResults();
    
    //Binding the datatable to your datagrid and showing the popup if required
    this.gvResults.DataSource = searchedRecords;
    this.gvResults.DataBind();

    if(searchRecords.Rows.Count == 0)
    {
       this.mpeMessage.show();
    }
}



I hope this will help you with your question. Feel free to shoot out anything required.

Happy coding..!!

Cheers,
Nayan
 
Share this answer
 
Comments
Ranjith Reddy CSE 13-May-13 1:47am    
or else Please give me solution using LABEL. for No Records found.
Hey Ranjith,

Label solution will be very simple.
Format your page as below: (CSS does count on your look and feel)

ASP.NET
<asp:label runat="server" id="lblTotalRecords" text="Total Records: 0" xmlns:asp="#unknown"></asp:label>
<asp:gridview runat="server" id="gvRecords" ...="" xmlns:asp="#unknown"></asp:gridview>


In your code behind:

C#
protected void SearchRecords()
{
    //code for connecting to the database and getting the results into DataTable
    //Assuming your are getting the results in the datatable named: dtResults

    this.gvRecords.DataSource = dtResults;
    this.gvRecords.DataBind();

    this.lblTotalRecords.Text = "Total Records: " + dtResults.Rows.Count.ToString();
}


This should be working as to show the total records within a label
I hope this should resolve your question.
Don't forget to mark the answer if it helped you.

Happy coding..!!

Regards,
Nayan
 
Share this answer
 
Comments
Ranjith Reddy CSE 13-May-13 2:05am    
Boss, i dont need total records to display..

Just I want to search the records, if not found Display a message "Record not found"

This is my code...

protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GVRecords.DataSource = ds;
GVRecords.DataBind();
con.Close();
}

Thanks.
Ranjith,

It could be even simpler.
Considering you have a label named: lblTotalRecords or something like that,
look at the code below:

protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
    {
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GVRecords.DataSource = ds;
        GVRecords.DataBind();
        con.Close();

        if(ds.Tables[0].Rows.Count == 0) //assuming ds is a DataSet
            this.lblTotalRecords.Text = "No Records Found..!!"

}


Hope this helps.

Regards,
Nayan
 
Share this answer
 
Comments
Ranjith Reddy CSE 13-May-13 2:29am    
thank you...........
C#
protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
    {
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds);
if(ds.Tables[0].Rows.Count == 0) 
{
ClientScript.RegisterStartupScript(Page.GetType(), "BtnSearch", "alert('Sorry  Record not found)')", true);
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GVRecords.DataSource = ds.Tables[0];
            GVRecords.DataBind();
            int columncount = GVRecords.Rows[0].Cells.Count;
            GVRecords.Rows[0].Cells.Clear();
            GVRecords.Rows[0].Cells.Add(new TableCell());
            GVRecords.Rows[0].Cells[0].ColumnSpan = columncount;
            GVRecords.Rows[0].Cells[0].Text = " Record not found !";
            GVRecords.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;

}
else
{
        GVRecords.DataSource = ds;
        GVRecords.DataBind();       
}
 con.Close();
}

I think ur problem solved by this...
 
Share this answer
 
v4
C#
public void Message(String msg)
   {
       string script = "window.onload = function(){ alert('";
       script += msg;
       script += "');";
       script += "window.location = '";
       script += "'; }";
       ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
   }
 
Share this answer
 
Make a little change in the existing code:

protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Table[0].Rows.Count > 0)
{
GVRecords.DataSource = ds;
GVRecords.DataBind();
}
else
{
Response.Write("alert('No Record Found!!!!')");
}
con.Close();
}
 
Share this answer
 

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