Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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# 如何解决这个问题;for loop";假设语句中的内容_C#_Sql Server - Fatal编程技术网

C# 如何解决这个问题;for loop";假设语句中的内容

C# 如何解决这个问题;for loop";假设语句中的内容,c#,sql-server,C#,Sql Server,我编写了嵌套的if语句。其中一个包含一个循环。在此循环之后,else语句不会像它们不存在一样工作。这是一个重要的声明。 虽然当我取消这个循环时,else语句仍然有效 我怎样才能修好它 这是VisualStudio和SQLServer2005。我已尝试将值保存在文本框“txtCategoryName”中。在第一个if语句中,我检查文本框是否为空。在第二个“if语句”中,我检查文本框中的值是否重复。在else语句中,我保存了值 try { if (txtCategoryName.Text ==

我编写了嵌套的if语句。其中一个包含一个循环。在此循环之后,else语句不会像它们不存在一样工作。这是一个重要的声明。 虽然当我取消这个循环时,else语句仍然有效

我怎样才能修好它

这是VisualStudio和SQLServer2005。我已尝试将值保存在文本框“txtCategoryName”中。在第一个
if
语句中,我检查文本框是否为空。在第二个“if语句”中,我检查文本框中的值是否重复。在
else
语句中,我保存了值

try
{
   if (txtCategoryName.Text == string.Empty)
   {
      MessageBox.Show("Fill the textBox", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    
      return;
   }
   else if (txtCategoryName.Text != string.Empty)
   {
      for (int i = 0; i <= dgvCategory.Rows.Count; i++)
      {
         if (dgvCategory.Rows[i].Cells[1].Value.ToString() == txtCategoryName.Text)
         {
            MessageBox.Show("Choose another name", "", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);    
            return;
         }
      }
   }
   else
   {
      txtCategoryId.Text = clsCat.getCategoryId().Rows[0][0].ToString();
      clsCat.addCategory(txtCategoryName.Text);
      MessageBox.Show("Done", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      txtCategoryId.Clear();
      txtCategoryName.Clear();
      dataPreview();
   }    
}
catch
{
   MessageBox.Show("Erroe save in category", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
   return;
}
试试看
{
if(txtCategoryName.Text==string.Empty)
{
MessageBox.Show(“填充文本框“,”,MessageBoxButtons.OK,MessageBoxIcon.惊叹号);
返回;
}
else if(txtCategoryName.Text!=string.Empty)
{

对于(int i=0;i只需添加一个
isElseLoop
bool变量,该变量将在执行
else循环后更新。请参阅下面的代码

private void btnSave_Click(object sender, EventArgs e)
{
  try
  {
    bool isElseLoop = false;

    if (txtCategoryName.Text == string.Empty)
    {
      MessageBox.Show("Fill the textBox", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      return;
    }
    else if (txtCategoryName.Text != string.Empty)
    {
      for (int i = 0; i < dgvCategory.Rows.Count; i++)
      {
        if (dgvCategory.Rows[i].Cells[1].Value.ToString() == txtCategoryName.Text)
        {
          MessageBox.Show("Choose another name", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          isElseLoop = true;
          return;
        }
      }
    }

    if (!isElseLoop)
    {
      txtCategoryId.Text = clsCat.getCategoryId().Rows[0][0].ToString();
      clsCat.addCategory(txtCategoryName.Text);
      MessageBox.Show("Done", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      txtCategoryId.Clear();
      txtCategoryName.Clear();
      dataPreview();
    }
  }
  catch
  {
    MessageBox.Show("Erroe save in category", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
  }
}
private void btnSave\u单击(对象发送者,事件参数e)
{
尝试
{
bool-isElseLoop=false;
if(txtCategoryName.Text==string.Empty)
{
MessageBox.Show(“填充文本框“,”,MessageBoxButtons.OK,MessageBoxIcon.惊叹号);
返回;
}
else if(txtCategoryName.Text!=string.Empty)
{
对于(int i=0;i
只需添加一个
isElseLoop
bool变量,该变量将在执行
else循环后更新。请参阅下面的代码

private void btnSave_Click(object sender, EventArgs e)
{
  try
  {
    bool isElseLoop = false;

    if (txtCategoryName.Text == string.Empty)
    {
      MessageBox.Show("Fill the textBox", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      return;
    }
    else if (txtCategoryName.Text != string.Empty)
    {
      for (int i = 0; i < dgvCategory.Rows.Count; i++)
      {
        if (dgvCategory.Rows[i].Cells[1].Value.ToString() == txtCategoryName.Text)
        {
          MessageBox.Show("Choose another name", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          isElseLoop = true;
          return;
        }
      }
    }

    if (!isElseLoop)
    {
      txtCategoryId.Text = clsCat.getCategoryId().Rows[0][0].ToString();
      clsCat.addCategory(txtCategoryName.Text);
      MessageBox.Show("Done", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      txtCategoryId.Clear();
      txtCategoryName.Clear();
      dataPreview();
    }
  }
  catch
  {
    MessageBox.Show("Erroe save in category", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
  }
}
private void btnSave\u单击(对象发送者,事件参数e)
{
尝试
{
bool-isElseLoop=false;
if(txtCategoryName.Text==string.Empty)
{
MessageBox.Show(“填充文本框“,”,MessageBoxButtons.OK,MessageBoxIcon.惊叹号);
返回;
}
else if(txtCategoryName.Text!=string.Empty)
{
对于(int i=0;i
问题出在
int i=0;i问题出在
int i=0;i如果您提供了错误消息,那就好了。我认为,for循环可能会导致索引超出范围异常。请调试它们。更改小于或等于小于only。&您的上一个else语句没有做任何操作,因此将外部bool变量维护为c检查您的条件是否通过或失败。如果通过,则将这些值插入DB。这是“转到
捕获
”这就是
try…catch
,to
try
的要点,如果它
抛出
s to
catch
异常
并处理或报告它。你只是默默地吞下
异常
,而不是在
消息中报告它的细节eBox
。您应该将
catch
更改为
catch(异常示例)
并在消息框中显示
ex
的一些属性,或在调试器中检查
ex
。通常,只要知道
异常的特定类型
就会指向有问题的行,否则堆栈跟踪肯定会。在构建之前是否尝试过清理解决方案?如果您提供了错误信息,则sg,这很好。我认为,您的for循环可能会导致索引超出范围异常。请调试它们。更改小于或等于小于only。&您的上一个else语句没有做任何操作,因此维护外部bool变量以检查您的条件是否通过或失败。如果通过,则将这些值插入DB。这是“转到
catch
"这就是
try…catch
,to
try
的要点,如果它
抛出
s to
catch
异常
并处理或报告它。你只是默默地吞下
异常
,而不是在
消息中报告它的细节eBox
。您应该将
catch
更改为
catch(异常示例)
并在消息框中显示
ex
的一些属性,或在调试器中检查
ex
。通常,只要知道
异常的特定类型,就会指向有问题的行,否则堆栈跟踪肯定会。您在构建之前尝试过清洗解决方案吗?加上一个,正如您解释的e问题,并且没有发布一堆格式错误的代码。更改后,“i