larie
larie

Reputation: 5

Incorrect syntax near '='.

I am trying to display my data on my website, using asp.net and c# but im getting this error, System.Data.SqlClient.SqlException: Incorrect syntax near '='. please help, i have double check my code but nothing seems to be wrong.

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RDCINFOS"].ConnectionString);
    SqlCommand cmd;
    SqlDataReader dr;
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {  
        SqlCommand cmd =new SqlCommand("select * from articles where id=" + Request.Params["x"], con);
        cmd.CommandType = CommandType.Text;
        con.Open();
        dr= cmd.ExecuteReader();
        while(dr.Read())
        {
            art.InnerHtml +=  "<br>" + dr["title"] + "</br><br>";
            art.InnerHtml +=  dr["details"] + "<br>";
            art.InnerHtml +=  "<img src=pict/" + dr["photo"] + " height=300 width=200/><br>";
        }

    }

Upvotes: 0

Views: 246

Answers (2)

Saif
Saif

Reputation: 2679

first of all, check if Request.Params["x"] is not null, and then change

SqlCommand cmd =new SqlCommand("select * from articles where id=" + Request.Params["x"], con);

to

SqlCommand cmd =new SqlCommand("select * from articles where id=@id", con);
cmd.Parameters.AddWithValue("@id", Request.Params["x"]);

Upvotes: 2

Sergio
Sergio

Reputation: 346

Request.Params["x"] possible empty, null or other type than id column in database

Upvotes: 0

Related Questions