错误“必须声明标量变量”@ searchkey" VS 2010 [英] error "must declare the scalar variable "@searchkey" VS 2010
本文介绍了错误“必须声明标量变量”@ searchkey" VS 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
收到错误信息。
请协助确定错误的来源。
Having the error message.
Please assist to identify where the error is coming from.
//INCREMENTAL SEARCH BEGINS
try
{
// Connection to the database
string str;
str = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(str);
//Call the statement
SqlCommand SqlCmd = new SqlCommand(@"Select * from Loanmaster where Id_Code LIKE @searchkey or Customer LIKE @searchkey or Apprs_No LIKE @searchkey", sqlCon);
SqlCmd.Parameters.AddWithValue("@searchkey", "%" + txt_Search.Text + "%");
//Open the sql data connection
sqlCon.Open();
//Execute the program
SqlCmd.ExecuteNonQuery();
//Display confirmation message
lblstatus.Text = "Search Result with string : " + txt_Search + "was sucessful";
//Clear all text boxes
txt_Acct1.Text = "";
txt_Acct2.Text = "";
txt_Acct3.Text = "";
SqlDataAdapter da = new SqlDataAdapter(@"Select * from Loanmaster where Id_Code LIKE @searchkey or Customer LIKE @searchkey or Apprs_No LIKE @searchkey", sqlCon);
DataSet ds = new DataSet();
da.Fill(ds, "MLOANJOIN");
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
txt_Acct1.Text = ds.Tables["MLOANJOIN"].Rows[0]["ACCT_NUMBER1"].ToString();
txt_Acct2.Text = ds.Tables["MLOANJOIN"].Rows[0]["ACCT_NUMBER2"].ToString();
txt_Acct3.Text = ds.Tables["MLOANJOIN"].Rows[0]["ACCT_NUMBER3"].ToString();
lblstatus.Text = "First record successful";
sqlCon.Close();
GridView1.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
txt_Id_Code.Enabled = false;
txt_Appraisal.Enabled = false;
txt_Customer.Focus();
sqlCon.Close();
}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}
btn_Add.Enabled = false;
//
//Updating the gridview and form end
//
//INCREMENTAL SEARCH ENDS
推荐答案
足够简单。在您的DataAdapter声明中,您通过在构造函数中指定SQL语句来创建新的SqlCommand对象,但您从未向其添加过SqlParameter。
坦率地说,你'重新执行完全相同的查询两次。我不知道为什么你甚至需要DataAdapter。
除此之外,请记住LIKE操作仅适用于文本字段,而不适用于数字字段。但是,由于我们不知道您的db列是什么类型,这只是一个建议。
Easy enough. In your DataAdapter declaration, you're creating a new SqlCommand object by specifying the SQL statement in the constructor but you never added a SqlParameter to it.
Frankly, you're executing the exact same query twice. I have no idea why you even need the DataAdapter.
On top of that, keep in mind that LIKE operation only works on text fields, not numeric fields. But, since we don't know what types your db columns are, this is only a suggestion.
您需要将参数添加到数据适配器选择命令,如下所示
You need to add parameter to the data adapter select command as below
da.SelectCommand.Parameters.AddWithValue("@searchkey", "%" + txt_Search.Text + "%");
或者你可以使用你已经创建的select命令
Or you can use select command you already created
SqlDataAdapter adapter = new SqlDataAdapter(SqlCmd);
DataSet ds = new DataSet();
da.Fill(ds, "MLOANJOIN");
GridView1.DataSource =ds.Tables[0];
GridView1.DataBind();
你在做什么?
让我解释你在代码中做了什么。
1.你正在执行相同的查询两次。
2.不确定为什么你是检索,因为你没有对第一次查询执行做任何事情。
3.下一步 - 第二次执行检索结果,你将它绑定到DataSet
。然后,您将在TextBoxes
中显示Row [0]
值。那么,其他行到底发生了什么?那是你想要实现的吗?
4.然后你绑定一个GridView
与另一个SqlDataSource
,这在这种情况下也是不合理的。
我的建议
所以,我建议你删除第一个查询执行代码。然后想想你究竟想做什么。调试代码并找出问题,
What are you doing?
Let me explain what you are doing in code.
1. You are executing the same query twice.
2. Not sure why exactly you are retrieving, because you are not doing anything with first query execution.
3. Next - the second execution retrieves you result and you are binding it to aDataSet
. Then you are showingRow[0]
values in theTextBoxes
. So, what exactly happens to other rows? Is that what you wanted to achieve?
4. Then you are binding oneGridView
with anotherSqlDataSource
, which is also not justified in this scenario.
My Suggestions
So, I suggest you to remove the first query execution code. Then think what exactly you want to do. Debug your code and find out the issue,
这篇关于错误“必须声明标量变量”@ searchkey" VS 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文