Reputation: 1
I have dropdownlist & button in master page, when button is clicked, I've to redirect to another page [results.aspx] that inherits the master page. Here is a gridview to bind data according to the selected item from dropdownlist. When I try to do, it throws
NullPointerException [Object reference not set to an instance of an object]
Help me with this.
This is what I did on page load event of the results.aspx.cs :
SqlConnection scon = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
DropDownList d1 = (DropDownList)Master.FindControl("DropDownList1");
DropDownList d2 = (DropDownList)Master.FindControl("DropDownList2");
DropDownList d3 = (DropDownList)Master.FindControl("DropDownList3");
SqlCommand scmd = new SqlCommand("select * from dreg where dcity='" + d2.SelectedItem.Text.ToString() + "' && dbg='" + d3.SelectedItem.Text.ToString() + "'", scon);
scmd.Connection = scon;
scon.Open();
SqlDataAdapter sad = new SqlDataAdapter(scmd);
SqlCommandBuilder scb = new SqlCommandBuilder(sad);
DataTable dTable = new DataTable();
sad.Fill(dTable);
GridView1.DataSource = dTable;
GridView1.DataBind();
}
}
Somewhere I heard that u ant find a previous page control if it is on master page because .net changes its id automatically. What can I do?
Upvotes: 0
Views: 3203
Reputation: 37543
FindControl
is not recursive at any level. It is unlikely that your DropDownList
belongs to the actual Master. You will need to call FindControl
from the panel or other control that actually has the DropDownList
. This can become murky since your children controls in the Master are not a part of its base type, so they are not accessible through the normal properties. My recommendation would be to add a property to your master page that directly references the DropDownList
:
// Do this with a unique name for each
// DropDownList you want to access
// You may have to give the property a
// a different name to prevent conflicts
public DropDownList MyDropDownList
{
get
{
return MyDropDownList;
}
}
Then in the pages that consume this Master page you will need to add this reference line in the html side of your page:
<%@ MasterType
virtualpath="~/Path/To/Your.master"
%>
This enables the compiler to understand the specific type of the master page being referenced, so in your code behind you can access the property directly without casting:
DropDownList myList = Master.MyDropDownList;
NOTE: Please, please, please research and begin using parameterized queries. Get in the habit of using them always, even while testing junk code. Bad habits like using inline sql have a nasty way of making it into production environments if you're not careful.
How do parameterized queries help against SQL injection?
http://www.dotnetperls.com/sqlparameter
http://blog.divergencehosting.com/?p=71
Upvotes: 0
Reputation: 1697
Your are trying to access Master Page Control so You Have to Try this :
DropDownList d1 = (DropDownList)Master.FindControl("DropDownList1");
DropDownList d2 = (DropDownList)Master.FindControl("DropDownList2");
DropDownList d3 = (DropDownList)Master.FindControl("DropDownList3");
SqlCommand scmd = new SqlCommand("select * from dreg where dcity='" + d2.SelectedItem.Text.ToString() + "' && dbg='" + d3.SelectedItem.Text.ToString() + "'", scon);
as you are accessing master Page Control. So Don't use page.PreviousPage
.Instead You should go with Master.FindControl
to access Master Page Control.
Upvotes: 2
Reputation: 1447
ClientIDMode ="Static", set this for the control then id should not change.
Upvotes: 0