Link to home
Start Free TrialLog in
Avatar of Tammu
Tammu

asked on

Getting error while using QueryStringParameter in asp.net 2.0

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

Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

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

Avatar of Tammu
Tammu

ASKER

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.

In your code connection close() part is missing.

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


ASKER CERTIFIED SOLUTION
Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tammu

ASKER

Thanks You SIr
Avatar of Tammu

ASKER

Thank you sir, You have nailed it. I appreciate it