如何纠正此错误“索引0为负数或高于行数。” [英] how to correct this error "Index 0 is either negative or above rows count."
本文介绍了如何纠正此错误“索引0为负数或高于行数。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void DropDownList1_TextChanged(对象发件人,EventArgs e)
{
SqlDataAdapter dam = new SqlDataAdapter( 从Net_calculate中选择*,其中DistrictName =' + DropDownList1.Text + ',con);
DataSet dsm = new DataSet();
// 添加此行以在数据集中定义表名
// dam.Tables.Add(Net_calculate);
dsm.Tables.Add( Net_calculate);
dam.Fill(dsm, Net_calculate);
DataView dvm = new DataView(dsm.Tables [ Net_calculate跨度>]);
txtAmt.Text = dvm [ 0 ] [ net_amount跨度>]的ToString();
}
解决方案
试试这个:
protected void DropDownList1_TextChanged( object sender,EventArgs e)
{
SqlCommand cmd = new SqlCommand( select * from Net_calculate(@dst),con);
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New Data.SqlClient.SqlParameter( @ dst,输入));
cmd.CommandTimeout = 0 ;
SqlDataAdapter dam = new SqlDataAdapter(cmd);
DataTable dtm = new DataTable( 测试跨度>);
// 添加此行以在数据集中定义表名
// dam.Tables.Add(Net_calculate);
dam.Fill(dtm);
if (dtm!= null ){
if (dtm .Rows.Count > 0 ){
txtAmt.Text = dtm .Rows [ 0 ] [ net_amount跨度>]的ToString();
}
}
}
--Amit
protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
SqlDataAdapter dam = new SqlDataAdapter("select * from Net_calculate where DistrictName='" + DropDownList1.Text + "'", con);
DataSet dsm = new DataSet();
// add this line to define table name in dataset
//dam.Tables.Add("Net_calculate");
dsm.Tables.Add("Net_calculate");
dam.Fill(dsm, "Net_calculate");
DataView dvm = new DataView(dsm.Tables["Net_calculate"]);
txtAmt.Text = dvm[0]["net_amount"].ToString();
}
解决方案
Try this:
protected void DropDownList1_TextChanged(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand("select * from Net_calculate(@dst)", con); cmd.CommandType = CommandType.Text cmd.Parameters.Add(New Data.SqlClient.SqlParameter("@dst", input)); cmd.CommandTimeout = 0; SqlDataAdapter dam = new SqlDataAdapter(cmd); DataTable dtm = new DataTable("Test"); // add this line to define table name in dataset //dam.Tables.Add("Net_calculate"); dam.Fill(dtm ); if(dtm != null){ if(dtm .Rows.Count > 0){ txtAmt.Text = dtm .Rows[0]["net_amount"].ToString(); } } }
--Amit
这篇关于如何纠正此错误“索引0为负数或高于行数。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文