Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web app (C#) which has a drop down list where users choose the week ending date for timesheets:-
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
 {
     Session["ChosenWeekEnding"] = DropDownList1.SelectedValue;
     RefreshTransactions(DropDownList1.SelectedValue);
     Response.Redirect(Request.RawUrl, true);
 }

RefreshTransactions executes the SQL query:-
protected void RefreshTransactions(string WE)
    {
        string WorkbenchConnection = ConfigurationManager.ConnectionStrings["WorkbenchConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(WorkbenchConnection);
        con.Open();
        string sqltext = "Select JobCode, ActivityCode, WorkCentreCode, TranDate FROM JobTransactions WHERE JobTranReference = '" + WE + "' AND PersonID = 3434 ";
        SqlCommand cmd = new SqlCommand(sqltext,con);
        SqlDataReader rdr = cmd.ExecuteReader();
        GridView1.DataSource = rdr;
        GridView1.DataBind();
        GridView1.Visible = true;
        con.Close();
    }

The first time the page loads the results are valid. However using the DropDownList to change the value of WE appears to do nothing; the page remains unchanged.
I'm new to web apps using SQL, what am I missing?

What I have tried:

I've moved the Response.Redirect into various other places in the code. I've searched extensively for examples to follow; in fact what I have done was from an example.
Posted
Updated 27-Oct-20 12:57pm
Comments
j snooze 27-Oct-20 17:53pm    
Before figuring this out, be sure to google and learn about parameterized queries, or you'll soon find out the wonders of SQL Injection.

Definitely use debug and break points to step through the code. Verify the value in the WE variable is what you expect from your drop down and that it should return something from SQL.
ormonds 27-Oct-20 18:20pm    
Thanks. Yes, the value of WE is correct, I have a temporary text box which displays the query. When I cut and past it into SQL it works fine.
I'll start looking at parameterised queries.

1 solution

what you are looking for is a cascading effect.

You have not tagged ASP.net webforms or MVC so would suggest you to look for how to do a partial page update. Look for ways that can be done - via javascript/jquery or updatepanel.

There are lots of examples on web.

P.S.:
1. You need to also look for parameterized query and make sure you use it
2. You don't need a response.redirect to update the same page to reload a grid data based on a change.
 
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