mscorlib.dll中发生未处理的“System.ArgumentOutOfRangeException”类型异常 [英] An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
本文介绍了mscorlib.dll中发生未处理的“System.ArgumentOutOfRangeException”类型异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void btnmodi_Click(对象发件人,EventArgs e)
{
if (radGridView1.RowCount < = 0 )
{
MessageBox.Show( 选择Record to Updae, );
return ;
}
如果(blUpdate == true )
{
btnmodi.Text = & Update;
blUpdate = false ;
txtCode.Text = radGridView1.SelectedCells [ 0 ]。Value.ToString();
txtcatagory.Text = radGridView1.SelectedCells [ 1 ]。Value.ToString();
btndele.Enabled = false ;
btnsav.Enabled = false ;
txtCode.Enabled = false ;
txtCode.Focus();
}
else
{
btnmodi.Text = & Edit;
blUpdate = true ;
{
SqlConnection cn = new SqlConnection(Program.myConnection);
string sql = 更新学生集代码= @ code,catagory = @ catagory,其中code = + radGridView1.SelectedCells [ 0 ]。Value.ToString();
SqlCommand cmd = new SqlCommand(sql,cn);
cmd.Parameters.AddWithValue( @ code ,txtCode.Text.Trim());
cmd.Parameters.AddWithValue( @ catagory,txtcatagory.Text.Trim() );
尝试
{
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show( 数据已在数据库中成功更新。, 更新记录,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex 。信息);
}
最后
{
cn.Close();
cmd.Dispose();
fillGrid();
btnsav.Enabled = true ;
btndele.Enabled = true ;
txtCode.Enabled = true ;
txtCode.Focus();
}
}
}
}
解决方案
< blockquote>可能是你正在使用的超出范围的索引,例如:
radGridView1.SelectedCells [ 1 ]
如果位置1没有元素(数组中的第二个元素),
将抛出该异常。
您可以通过调试来提供更多信息,以查看抛出异常的确切行。
干杯,
C
private void btnmodi_Click(object sender, EventArgs e)
{
if (radGridView1.RowCount <= 0)
{
MessageBox.Show(" Select Record to Updae","" );
return;
}
if (blUpdate == true)
{
btnmodi.Text = "&Update";
blUpdate = false;
txtCode.Text = radGridView1.SelectedCells[0].Value.ToString();
txtcatagory.Text = radGridView1.SelectedCells[1].Value.ToString();
btndele.Enabled = false;
btnsav.Enabled = false;
txtCode.Enabled = false;
txtCode.Focus();
}
else
{
btnmodi.Text = "&Edit";
blUpdate = true;
{
SqlConnection cn = new SqlConnection(Program.myConnection);
string sql = "update student set code=@code,catagory=@catagory, where code=" + radGridView1.SelectedCells[0].Value.ToString();
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@code", txtCode.Text.Trim());
cmd.Parameters.AddWithValue("@catagory", txtcatagory.Text.Trim());
try
{
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Data has been update successfully in database.", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
cmd.Dispose();
fillGrid();
btnsav.Enabled = true;
btndele.Enabled = true;
txtCode.Enabled = true;
txtCode.Focus();
}
}
}
}
解决方案
Might be an index you are using that is out of bounds, e.g.:
radGridView1.SelectedCells[1]
will throw that exception if there is no element at position 1 (second element in the array).
You can provide more information by debugging to see the exact line in which the exception is being thrown.
Cheers,
C
这篇关于mscorlib.dll中发生未处理的“System.ArgumentOutOfRangeException”类型异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文