Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自另一个空的变量_C#_Variables_Void - Fatal编程技术网

C# 来自另一个空的变量

C# 来自另一个空的变量,c#,variables,void,C#,Variables,Void,我是一个新手程序员(c#),我被卡住了。我不知道为什么编译器没有从另一个空中看到变量。大约一周前,在另一个项目中,它成功了,但现在不想。我正在寻找一个类似的问题,但我没有发现 public void Gridcommandevent(object sender, GridViewCommandEventArgs ev) { int index; if (ev.CommandName == "Update") { index = Convert.ToInt32

我是一个新手程序员(c#),我被卡住了。我不知道为什么编译器没有从另一个空中看到变量。大约一周前,在另一个项目中,它成功了,但现在不想。我正在寻找一个类似的问题,但我没有发现

public void Gridcommandevent(object sender, GridViewCommandEventArgs ev)
{
    int index;
    if (ev.CommandName == "Update")
    {
       index = Convert.ToInt32(ev.CommandArgument);
    }
}

protected void GridView3_RowCommand(object sender, GridViewUpdateEventArgs e)
{   
    String strConnString = ConfigurationManager.ConnectionStrings["DGCodLocConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);

    con.Open();
    SqlCommand cmd = new SqlCommand("UPDATE Parts SET [LastModified] = '" + Permit + "' WHERE PartsID = '" + index + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
我想将
索引
变量添加到
SqlCommand
对象中

顺便说一下。这是好代码吗?这将是工作吗?在更新gridview中的数据(行)后,我想在单元格[LastModified]中插入变量“Permit”


这似乎并没有达到应有的效果:(我的意思是,变量“index”现在没问题,但代码刚刚被破坏。

在您的void之外声明您的变量,这样您就可以从所有成员访问它。当您拥有它时,它只对
Gridcommandevent
可见

int index;
public void Gridcommandevent(object sender, GridViewCommandEventArgs ev)
{
    if (ev.CommandName == "Update")
    {
       index = Convert.ToInt32(ev.CommandArgument);
    }
}

protected void GridView3_RowCommand(object sender, GridViewUpdateEventArgs e)
{   
    String strConnString = ConfigurationManager.ConnectionStrings["DGCodLocConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);

        con.Open();
        SqlCommand cmd = new SqlCommand("UPDATE Parts SET [LastModified] = '" + Permit + "' WHERE PartsID = '" + index + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
}

根据Andreas Niedermair的建议,既然你说你是一个新手程序员,你应该看看这个

考虑使用参数化查询,不要对sql注入太开放…!但是没有一个文本框不是真的重要。命名它为一致性或其他什么,但是第一个时刻不要停止使用参数是万恶之源。不,不,不……OP显然是一个绝对的初学者。OP问的是“为什么”,而不是“如何”…@Andreasniedermir看他问题的最后一行。他想要一种访问变量的方法……和(s)他不知道为什么不能从另一种方法中获得……考虑到你至少应该提到的技能水平scopes@AndreasNiedermair既然你在问题的评论中已经提到了这一点,如果我在回答中使用链接,你还好吗?出于价值考虑,你是对的,我应该更深入地了解可变可见性艾克。我只是看了很久这个代码。谢谢你的回答
int index;
public void Gridcommandevent(object sender, GridViewCommandEventArgs ev)
{
    if (ev.CommandName == "Update")
    {
       index = Convert.ToInt32(ev.CommandArgument);
    }
}

protected void GridView3_RowCommand(object sender, GridViewUpdateEventArgs e)
{   
    String strConnString = ConfigurationManager.ConnectionStrings["DGCodLocConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);

        con.Open();
        SqlCommand cmd = new SqlCommand("UPDATE Parts SET [LastModified] = '" + Permit + "' WHERE PartsID = '" + index + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
}