Click here to Skip to main content
15,903,804 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the issuse is that i am using on click event on button but when i click on button noting happen but page refresh

protected void btnGenerateReport_Click(object sender, EventArgs e)
       {
           try
           {
               // Validate input fields
               if (string.IsNullOrEmpty(from_date.Text))
               {
                   throw new Exception("Please enter a start date.");
               }

               if (string.IsNullOrEmpty(to_date.Text))
               {
                   throw new Exception("Please enter an end date.");
               }

               if (ddlExpenseType.SelectedValue != "company_expense")
               {
                   throw new Exception("Please select a company expense type.");
               }

               // Create a new SqlConnection object
               using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
               {
                   // Open the database connection
                   con.Open();

                   // Define SQL query to select data from the table
                   string query = "SELECT [id], [name], [ptclno], [mobile1], [mobile2], [address], [ntn_no], [str_no], [created_date] FROM [gidsolcms].[dbo].[tblbusiness_info]";

                   // Create a new SqlCommand object
                   using (SqlCommand command = new SqlCommand(query, con))
                   {
                       // Execute the query and get the SqlDataReader object
                       using (SqlDataReader reader = command.ExecuteReader())
                       {
                           // Read the first row of data from the SqlDataReader
                           if (reader.Read())
                           {
                               // Store the values of each column in separate variables
                               int id = (int)reader["id"];
                               string name = (string)reader["name"];
                               string ptclno = (string)reader["ptclno"];
                               string mobile1 = (string)reader["mobile1"];
                               string mobile2 = (string)reader["mobile2"];
                               string address = (string)reader["address"];
                               string ntn_no = (string)reader["ntn_no"];
                               string str_no = (string)reader["str_no"];
                               DateTime created_date = (DateTime)reader["created_date"];

                               // Fetch report data using stored procedure
                               DataSet ds4 = new DataSet();
                               using (SqlCommand cmd = new SqlCommand("sp_rpt_expsummary", con))
                               {
                                   cmd.CommandType = CommandType.StoredProcedure;
                                   cmd.Parameters.AddWithValue("@from_date", Convert.ToDateTime(from_date.Text));
                                   cmd.Parameters.AddWithValue("@to_date", Convert.ToDateTime(to_date.Text));
                                   cmd.Parameters.AddWithValue("@expense_type", ddlExpenseType.SelectedValue);

                                   SqlDataAdapter da4 = new SqlDataAdapter(cmd);
                                   da4.Fill(ds4);
                               }

                               // Load report and set parameters
                               ReportDocument rRep = new ReportDocument();
                               string reportPath = Server.MapPath("~/Reports/rptexpsummary.rpt");
                               rRep.Load(reportPath);
                               rRep.SetDataSource(ds4.Tables[0]);
                               rRep.SetParameterValue("dfrom", Convert.ToDateTime(from_date.Text).ToString());
                               rRep.SetParameterValue("dto", Convert.ToDateTime(to_date.Text).ToString());
                               rRep.SetParameterValue("c_name", name);
                               rRep.SetParameterValue("c_address", address);
                               rRep.SetParameterValue("c_ptcl", ptclno);
                               rRep.SetParameterValue("e_type", ddlExpenseType.SelectedItem.ToString());
                               rRep.SetParameterValue("entry_type", ddlExpenseType.SelectedItem.ToString());

                               // Display report


                               CrystalReportViewer1.ReportSource = rRep;
                           }
                       }
                   }
               }

           }
           catch (Exception ex)
           {
               // handle the exception that was thrown
               lblError.Text= ("An error occurred: " + ex);
           }

       }


What I have tried:

i have tryed all exception and validation noting work as iam showing in discription
the error in inspect console log of firefox browser are
<pre>GEThttp://localhost:49540/UI/cristal report.aspx?ctl00$ContentPlaceHolder1$from_date=&ctl00$ContentPlaceHolder1$to_date=&ctl00$ContentPlaceHolder1$ddlExpenseType=0&ctl00$ContentPlaceHolder1$btnGenerateReport=FIND
[HTTP/1.1 200 OK 191ms]

GEThttp://localhost:49540/WebResource.axd?d=pynGkmcFUV13He1Qd6_TZGHZNshPCaw0H_1j8tEijTpK2XZSX-hpFifobwZxOjqv0-XV5-XNJS0I8TSeuAIjnA2&t=637814840020000000
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/crv.js
[HTTP/1.1 404 Not Found 104ms]

GEThttp://localhost:49540/aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/images/style.css
[HTTP/1.1 404 Not Found 103ms]

GEThttp://localhost:49540/UI/lib/chart/chart.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/easing/easing.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/waypoints/waypoints.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/owlcarousel/owl.carousel.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/moment.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/moment-timezone.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/tempusdominus-bootstrap-4.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/js/main.js
[HTTP/1.1 200 OK 0ms]

GEThttps://code.jquery.com/jquery-3.4.1.min.js
[HTTP/2 200 OK 0ms]

GEThttps://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js
[HTTP/2 200 OK 0ms]

GEThttp://localhost:49540/aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/crv.js
[HTTP/1.1 404 Not Found 2ms]

Loading failed for the <script> with source "http://localhost:49540/aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/crv.js". cristal report.aspx:62:1
GEThttp://localhost:49540/aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/images/style.css
[HTTP/1.1 404 Not Found 4ms]

Uncaught TypeError: $(...).get(...) is undefined
    <anonymous> http://localhost:49540/UI/js/main.js:68
    <anonymous> http://localhost:49540/UI/js/main.js:211
main.js:68:38
GEThttps://code.jquery.com/jquery-3.4.1.min.js
[HTTP/2 200 OK 0ms]

GEThttps://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js
[HTTP/2 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/chart/chart.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/easing/easing.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/waypoints/waypoints.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/owlcarousel/owl.carousel.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/moment.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/moment-timezone.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/lib/tempusdominus/js/tempusdominus-bootstrap-4.min.js
[HTTP/1.1 200 OK 0ms]

GEThttp://localhost:49540/UI/js/main.js
[HTTP/1.1 200 OK 0ms]

Uncaught TypeError: $(...).get(...) is undefined
    <anonymous> http://localhost:49540/UI/js/main.js:68
    <anonymous> http://localhost:49540/UI/js/main.js:211
main.js:68:38
Uncaught ReferenceError: bobj is not defined
    <anonymous> http://localhost:49540/UI/cristal report.aspx?ctl00$ContentPlaceHolder1$from_date=&ctl00$ContentPlaceHolder1$to_date=&ctl00$ContentPlaceHolder1$ddlExpenseType=0&ctl00$ContentPlaceHolder1$btnGenerateReport=FIND:335
cristal report.aspx:335:1
GEThttp://localhost:49540/UI/img/Gid.png
[HTTP/1.1 404 Not Found 0ms]
Posted
Updated 19-Feb-23 23:04pm
Comments
muhammad abubakar 2022 20-Feb-23 6:47am    
Just show vALIDATION AND OUT REFRESH WHEN I CLICK BTN FIND
i remove all the code from btnGenerateReport_Click function and add validation to text box like





<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" errormessage="plz Enter TO date " controltovalidate="to_date" forecolor="#FF3300" validationgroup="i1">

<asp:textbox id="from_date" class="form-control" placeholder="From Date" type="date" runat="server">




<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" controltovalidate="from_date" errormessage="PLZ ENTER FROM DATE" forecolor="#CC0000" validationgroup="i1">
<asp:textbox id="to_date" class="form-control" placeholder="From Date" type="date" runat="server">






Expense Type :
<asp:label id="Label2" runat="server">

<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" controltovalidate="ddlExpenseType" errormessage="sELECT EXPENSE" forecolor="Red" validationgroup="i1">

<asp:dropdownlist id="ddlExpenseType" runat="server">
<asp:listitem value="0">Company Expense
<asp:listitem value="1">Home Expense

<asp:button id="btnGenerateReport" class="btn btn-primary" runat="server" text="FIND" onclick="btnGenerateReport_Click" forecolor="#FFFFCC" validationgroup="i1">


<asp:label id="lblError" runat="server" text=" I will Show Error">

1 solution

If there's "nothing happening", then there's no data being returned from your first query. You'll need to debug your code to find out why.

NB: Throwing and catching an exception is an extremely poor way of handling a "missing required field" condition. It would be far better to simply exit the method if the fields are not provided:
C#
// Validate input fields
if (string.IsNullOrEmpty(from_date.Text))
{
    lblError.Text = "Please enter a start date.";
    return;
}

if (string.IsNullOrEmpty(to_date.Text))
{
    lblError.Text = "Please enter an end date.";
    return;
}

if (ddlExpenseType.SelectedValue != "company_expense")
{
    lblError.Text = "Please select a company expense type.";
    return;
}
Or better yet, use the validation controls to give your users feedback on all validation errors at once:
Walkthrough: Validating User Input in a Web Forms Page | Microsoft Learn[^]
 
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