Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 sqlparameter

Author  Topic 

samir80
Starting Member

10 Posts

Posted - 2008-07-10 : 17:58:47
Hi everyone-

I have following code:



SqlConnection conn = new SqlConnection(xyz);

SqlCommand myCommand = new SqlCommand();

myCommand.CommandType = System.Data.CommandType.Text;

myCommand.Connection = conn;

sSQL = "SELECT * FROM abc WHERE abcID <> 26 ";

if (current.Request["lmn"] != null && current.Request["lmn"] != "")

{

SqlParameter parameterlmn = new SqlParameter("@lmn", SqlDbType.NVarChar, 50);

parameterlmn .Value = "'" + current.Request["lmn"] + "'";

myCommand.Parameters.Add(parameterlmn);

sSQL += " AND lmn LIKE @lmn ";


}

myCommand.CommandText = sSQL;





SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

conn.Open();

DataSet ds = new DataSet();

myAdapter.Fill(ds, "Products");

conn.Close();

myAdapter.Dispose();

return ds;





The problem is when i return that dataset ds, its giving me following error :

System.Data.SqlClient.SqlException: Must declare the variable '@lmn'.

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)



Any ideas???

Sitka
Aged Yak Warrior

571 Posts

Posted - 2008-07-11 : 10:10:40
sSQL = "SELECT * FROM abc WHERE abcID <> 26 ";

if (current.Request["lmn"] != null && current.Request["lmn"].ToString().Trim() != "")

{

sSQL += "AND lmn LIKE '%" + current.Request["lmn"].ToString() +"%'";


}

myCommand.CommandText = sSQL;


Or an alternative style....


SqlConnection conn = new SqlConnection(xyz);

SqlCommand myCommand = new SqlCommand();

myCommand.Connection = conn;

myCommand.CommandType = System.Data.CommandType.StoredProcedure;

myCommand.CommandText = "GetStuffBylmn;

if (current.Request["lmn"] != null && current.Request["lmn"].ToString().Trim() != "")

{

SqlParameter parameterlmn = new SqlParameter("@lmn", SqlDbType.NVarChar, 50);

parameterlmn .Value = current.Request["lmn"].ToString().Trim();

myCommand.Parameters.Add(parameterlmn);

}

"it's definitely useless and maybe harmful".
Go to Top of Page
   

- Advertisement -