使用未分配的局部变量'mySqlCommand' [英] Use of unassigned local variable 'mySqlCommand'

查看:102
本文介绍了使用未分配的局部变量'mySqlCommand'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试应用dcuments但发生以下错误



  protected   void  Button1_Click( object  sender,EventArgs e)
{

string connStr = ConfigurationManager.ConnectionStrings [ mydms ]的ConnectionString。
SqlConnection mySQLconnection = new SqlConnection(connStr);
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
// DropDownList drdList;
SqlCommand mySqlCommand;


foreach (GridViewRow row in GrdFileApprove.Rows)
{

if (row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownListcontrol = row .FindControl( DropDownList4 as 下拉列表;

SqlCommand cmd = new SqlCommand( approved,mySQLconnection);

docc.approve(Convert.ToInt32(会话[ UserID] ),Convert.ToInt32(会话[ DocID]),Convert.ToInt32(Session [ ApproveID]),Convert.ToString(Session [ Login2]));


mySqlCommand.Parameters.Add( @ UserID ,SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys [row.RowIndex] [ UserID]);

mySqlCommand.Parameters.Add( @ DocID,SqlDbType.Int ).Value = Convert.ToInt32(GrdFileApprove.DataKeys [row.RowIndex] [ DocID]) ;

mySqlCommand.Parameters.Add( @ ApproveID,SqlDbType.Int ).Value = DropDownListcontrol.SelectedValue;

// docc.approve(Convert.ToInt32(Session [UserID])), Convert.ToInt32(Session [DocID]),Convert.ToInt32(Session [ApproveID]),Convert.ToString(Session [Login2]));

mySqlCommand .ExecuteNonQuery();
}
else
{
apfi.Text = 错误;
}


}


如果(mySQLconnection.State = = ConnectionState.Open)
{
mySQLconnection.Close();
}
}





此行错误



  mySqlCommand  .Parameters.Add(  @ UserID,SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys [row.RowIndex] [ 用户ID]); 

解决方案

嘿那里,



很明显在你的代码中,你声明了一个 SqlCommand 变量

SqlCommand mySqlCommand; 但是使用它没有在这里创建它的对象:

 mySqlCommand.Parameters.Add(  @ UserID,SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys [row.RowIndex] [  UserID ]); 



你在这里创建一个 SqlCommand 的对象:

 SqlCommand cmd =  new  SqlCommand(  approved,mySQLconnection); 

但从未使用它。



将以上行更改为:

 

mySqlCommand = new SqlCommand(approved,mySQLconnection);



如果有帮助请告诉我,



Azee ...


将cmd改为mySqlCommand,反之亦然。



 SqlCommand mySqlCommand = new SqlCommand(approved,mySQLconnection); 

docc.approve(Convert.ToInt32(Session [UserID]),Convert.ToInt32(Session [DocID]),Convert.ToInt32(Session [ApproveID]),转换。的ToString(会话[ Login2身份]));


i try to apprve dcuments but the following error occur

protected void Button1_Click(object sender, EventArgs e)
        {

            string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
            SqlConnection mySQLconnection = new SqlConnection(connStr);
            if (mySQLconnection.State == ConnectionState.Closed)
            {
                mySQLconnection.Open();
            }
               // DropDownList drdList;
            SqlCommand mySqlCommand;


            foreach (GridViewRow row in GrdFileApprove.Rows)
            {
              
                if (row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList DropDownListcontrol = row.FindControl("DropDownList4") as DropDownList;

                    SqlCommand cmd = new SqlCommand("approved", mySQLconnection);
                    
                    docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));

                   
                    mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);

                    mySqlCommand.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["DocID"]);

                    mySqlCommand.Parameters.Add("@ApproveID", SqlDbType.Int).Value = DropDownListcontrol.SelectedValue;

                    //docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));

                    mySqlCommand.ExecuteNonQuery(); 
                }
                else
                {
                    apfi.Text = "Error";
                }

                
            }


            if (mySQLconnection.State == ConnectionState.Open)
            {
                mySQLconnection.Close();
            }
        }



error in this line

mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);

解决方案

Hey there,

Its quite obvious in your code, you have declared an SqlCommand variable
SqlCommand mySqlCommand; but used it without creating its object here:

mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);


and you are creating an object of SqlCommand here:

SqlCommand cmd = new SqlCommand("approved", mySQLconnection);

but never used it.

Change the above line to:

mySqlCommand = new SqlCommand("approved", mySQLconnection);

Let me know if it helps,

Azee...


Change "cmd" to "mySqlCommand" or vice versa.

SqlCommand mySqlCommand = new SqlCommand("approved", mySQLconnection);
                    
                    docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));


这篇关于使用未分配的局部变量'mySqlCommand'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆