在c#的where子句中检索特定的值
本文关键字:检索 子句 where | 更新日期: 2023-09-27 18:07:37
我想在代码中获得cmdd(@PermissionAccessoageID)值,以及如何实现这一目标。
我得到这个错误
我代码:类型为'System '的异常。在System.Data.dll中发生了ArgumentException,但没有在用户代码中处理
附加信息:不存在从对象类型System.Data.SqlClient.SqlCommand到已知托管提供程序本机类型的映射。
protected void Add_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Pages") });
foreach (GridViewRow row in addGridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[1].FindControl("chkboxaddView") as CheckBox);
CheckBox chkRow1 = (row.Cells[2].FindControl("chkboxaddEdit") as CheckBox);
string aaa = row.Cells[0].Text;
if (chkRow.Checked)
{
con.Open();
var vaala = chkRow.Checked ? 1 : 0;
var vaalaa = chkRow1.Checked ? 1 : 0;
var val = chkaddisactive.Checked ? 1 : 0;
var vall = chkboxisactive.Checked ? 1 : 0;
string sqlcmd = "select AccesspageID From AccessPages where pages='" + row.Cells[0].Text + "'";
SqlCommand cmdd = new SqlCommand(sqlcmd, con);
cmdd.CommandType = CommandType.Text;
cmdd.ExecuteScalar();
string query = "add3tables"; //Stored procedure name
SqlCommand com = new SqlCommand(query, con); //creating SqlCommand object
com.CommandType = CommandType.StoredProcedure; //here we declaring command type as stored procedure
// adding parameters to SqlCommand below
com.Parameters.AddWithValue("@Name", txtaddrolename.Text.ToString());
string unique_string = Guid.NewGuid().ToString();
com.Parameters.AddWithValue("@RoleID ", unique_string);
com.Parameters.AddWithValue("@IsActive ", val);
com.Parameters.AddWithValue("@PermissionRoleID ", unique_string);
com.Parameters.AddWithValue("@View ", vaala);
com.Parameters.AddWithValue("@Edit ", vaalaa);
com.Parameters.AddWithValue("@PermissionAccessoageID ", cmdd);
com.ExecuteNonQuery();
con.Close();
}
if (chkRow1.Checked == false)
{
var vaala = chkRow.Checked ? 1 : 0;
var vaalaa = chkRow1.Checked ? 1 : 0;
var val = chkaddisactive.Checked ? 1 : 0;
var vall = chkboxisactive.Checked ? 1 : 0;
SqlCommand cmdd = new SqlCommand("select AccesspageID From AccessPages where pages='" + aaa + "'", con);
con.Open();
cmdd.CommandType = CommandType.Text;
string query = "add3tables"; //Stored procedure name
SqlCommand com = new SqlCommand(query, con); //creating SqlCommand object
com.CommandType = CommandType.StoredProcedure; //here we declaring command type as stored procedure
// adding parameters to SqlCommand below
com.Parameters.AddWithValue("@Name", txtaddrolename.Text.ToString());
string unique_string = Guid.NewGuid().ToString();
com.Parameters.AddWithValue("@RoleID ", unique_string);
com.Parameters.AddWithValue("@IsActive ", val);
com.Parameters.AddWithValue("@PermissionRoleID ", unique_string);
com.Parameters.AddWithValue("@View ", vaala);
com.Parameters.AddWithValue("@Edit ", vaalaa);
com.Parameters.AddWithValue("@PermissionAccessoageID ", cmdd);
com.ExecuteNonQuery();
con.Close();
}
}
您忘记使用select语句的结果,需要将其赋值给一个变量。
在第一个if块中实际执行它,但不使用结果。在第二个if块中,您忘记了.ExecuteScalar()
调用。
object oAccesspageID = cmdd.ExecuteScalar();
int? AccesspageID = (int?)(!Convert.IsDBNull(oAccesspageID) ? oAccesspageID : null);
/* ... code ommitted*/
//assuming @PermissionAccessoageID is int
com.Parameters.AddWithValue("@PermissionAccessoageID ", AccesspageID.Value);
由于ExecuteScalar可以返回null,如果没有找到结果,您需要以某种方式对其进行解释。您可能需要在调用SProc之前添加另一个if(AccesspageID.HasValue) { }
调用,以确保它不会尝试使用无效值运行。
这一行就是问题所在:
com.Parameters.AddWithValue("@PermissionAccessoageID ", cmdd);
cmdd
为SqlCommand
类型,不能设置该类型的参数