Reputation: 2855
protected void Page_Load(object sender, EventArgs e)
{
string query = "SELECT Posts.*, Import_Export.* FROM Import_Export INNER JOIN Posts ON Import_Export.CatID = Posts.CatID where Import_Export.Parent=@Parent ";
if (Request.QueryString["CID"] != null)
{
query += " and Import_Export.CatID=@CatID";
SqlDataSource2.SelectCommand = query;
SqlDataSource2.SelectParameters["Parent"].DefaultValue = Request.QueryString["Type"].ToString();
SqlDataSource2.SelectParameters["CatID"].DefaultValue = Request.QueryString["CID"].ToString();
}
else
{
SqlDataSource2.SelectCommand = query;
SqlDataSource2.SelectParameters["Parent"].DefaultValue = Request.QueryString["Type"].ToString();
}
}
hi
I write this code and when I run this code below error was occurred:
Object reference not set to an instance of an object.
Why?
Upvotes: 0
Views: 980
Reputation: 33
Try this
protected void Page_Load(object sender, EventArgs e)
{
string query = "SELECT Posts.*, Import_Export.* FROM Import_Export INNER JOIN Posts ON Import_Export.CatID = Posts.CatID where Import_Export.Parent=@Parent ";
if (Request.QueryString != null)
{
Parameter par = new Parameter("@Parent", DbType.String, Request.QueryString["Type"].ToString());
SqlDataSource2.SelectParameters.Add(par);
if (Request.QueryString["CID"] != null)
{
query += " and Import_Export.CatID=@CatID";
SqlDataSource2.SelectCommand = query;
Parameter par2 = new Parameter("@CatID", DbType.String, Request.QueryString["CID"].ToString());
SqlDataSource2.SelectParameters.Add(par2);
}
else
{
SqlDataSource2.SelectCommand = query;
}
}
}
Upvotes: 0
Reputation: 399
To sum up here's what it should look like
SqlDataSource2.SelectCommand.Parameters.Add("Parent", System.Data.SqlDbType.NVarChar);
SqlDataSource2.SelectCommand.Parameters["Parent"].Value = Request.QueryString["Type"].ToString();
OR
SqlDataSource2.SelectCommand.Parameters.AddWithValue("Parent",Request.QueryString["Type"].ToString());
Upvotes: 2