[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting error while using QueryStringParameter in asp.net 2.0

Posted on 2009-04-17
6
Medium Priority
?
335 Views
Last Modified: 2013-11-07
Hi Experts,
i am trying to adding page title dynamically from a table which is stored in SQL database. here is my code but i am getting the error as :

Must declare the scalar variable "@ptitle"'
and the code is breaking at: SqlCmd.ExecuteNonQuery();

I have no idea where i am making the mistake.
can someone please guide. I appreciate it



protected void Page_Load(object sender, EventArgs e)
    {
        QueryStringParameter.Equals("Ptitle", @ptitle);
		string query = "select * from PageInfo where Ptitle = @ptitle ";
        SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MySiteConnectString"].ConnectionString);
        SqlCommand SqlCmd = null;
        SqlCmd = new SqlCommand(query, myconnection);
        SqlCmd.Connection.Open();
        SqlCmd.ExecuteNonQuery();
 
        SqlDataAdapter ad = new SqlDataAdapter(SqlCmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
 
 
        title.InnerHtml = dt.Rows[0]["Ptitle"].ToString();
        description.Attributes.Add("content" ,dt.Rows[0]["Desc"].ToString());
       
 
    }

Open in new window

0
Comment
Question by:niceoneishere
6 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24170887
You have wrote your code wrongly, here is how it must be from line 3 - 9:

        QueryStringParameter q = new QueryStringParameter("@Ptitle", "ptitle");
                string query = "select * from PageInfo where Ptitle = @ptitle ";
        SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MySiteConnectString"].ConnectionString);
        SqlCommand SqlCmd = null;
        SqlCmd = new SqlCommand(query, myconnection);
        SqlCmd.Parameters.Add(q);
        SqlCmd.Connection.Open();
        SqlCmd.ExecuteNonQuery();

Open in new window

0
 
LVL 2

Author Comment

by:niceoneishere
ID: 24173238
Hi Sir,
i am getting this error now :
The SqlParameterCollection only accepts non-null SqlParameter type objects, not QueryStringParameter objects
what am i doing wrong.
Thanks i appreciate it.

0
 
LVL 12

Expert Comment

by:Nirmalan Nagenthiran
ID: 24174183
In your code connection close() part is missing.

Check this post might useful.
http://www.tek-tips.com/viewthread.cfm?qid=949066&page=6


0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 2000 total points
ID: 24175341
QueryStringParameter can't be added into SqlParameterCollection. QueryStringParameter can be added directly into SqlDataSource, AccessDataSource objects.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.querystringparameter.aspx

So you code has to be changed as
        SqlParameter q = new SqlParameter("@Ptitle", Request["ptitle"]);
                string query = "select * from PageInfo where Ptitle = @ptitle ";
        SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MySiteConnectString"].ConnectionString);
        SqlCommand SqlCmd = null;
        SqlCmd = new SqlCommand(query, myconnection);
        SqlCmd.Parameters.Add(q);
        myconnection.Open();
        SqlCmd.ExecuteNonQuery();
        myconnection.Close();

Open in new window

0
 
LVL 2

Author Closing Comment

by:niceoneishere
ID: 31571596
Thanks You SIr
0
 
LVL 2

Author Comment

by:niceoneishere
ID: 24185377
Thank you sir, You have nailed it. I appreciate it
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question