DataRow row = dt.Rows [0]; //位置0 indexoutofrangeexception没有行

时间:2012-01-03 13:10:08

标签: asp.net

SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["GVRAgencyConnectionString1"].ConnectionString);
            con1.Open();
            SqlDataAdapter da = new SqlDataAdapter("select      weight,mrp_pack,no_of_pack,mrp_master_pack from dbo.purchase_order where   product_ID=@product_ID",con1);
            da.SelectCommand.Parameters.AddWithValue("@product_ID", DropDownList3.Text);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con1.Close();           
            DataRow row = dt.Rows[0];
            txtwgt.Text = row["weight"].ToString();
            txtmrpsinglepack.Text = row["mrp_pack"].ToString();
            txtnoofmasterpack.Text = row["no_of_pack"].ToString();
            txtmrpmaster.Text = row["mrp_master_pack"].ToString();

3 个答案:

答案 0 :(得分:2)

1)试试这个:

2)欢迎来到StackOverFlow。

3)如果你没有女孩,你就不能和一个女孩住在一起。

相同 - 如果您不知道索引0中已有行,则无法触及datatable withindex中的记录。

SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["GVRAgencyConnectionString1"].ConnectionString);
            con1.Open();
            SqlDataAdapter da = new SqlDataAdapter("select      weight,mrp_pack,no_of_pack,mrp_master_pack from dbo.purchase_order where   product_ID=@product_ID",con1);
            da.SelectCommand.Parameters.AddWithValue("@product_ID", DropDownList3.Text);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con1.Close();           
             if (dt.Rows.Count==0) return;
            DataRow row = dt.Rows[0];
            txtwgt.Text = row["weight"].ToString();
            txtmrpsinglepack.Text = row["mrp_pack"].ToString();
            txtnoofmasterpack.Text = row["no_of_pack"].ToString();
            txtmrpmaster.Text = row["mrp_master_pack"].ToString();

答案 1 :(得分:1)

您的结果为空,因此数据表中根本没有行。

您可以在尝试访问该行之前进行检查:

if (dt.Rows.Count > 0)  {
  ...
}

答案 2 :(得分:0)

OracleConnection con = new OracleConnection(ConnectionString);
string selectquery = "SELECT TITLE,DESCRIPTION,SEVERITY,STATUS,IMPACT,USER_NAME AS CREATED_BY ,CREATED_DATE FROM Issues A, USERS B  Where  A.CREATED_BY=B.USER_ID AND A.ISSUE_ID=" + ID;
//string selectquery = "SELECT TITLE,DESCRIPTION,USER_NAME AS ASSIGNED_TO,SEVERITY,STATUS,IMPACT,USER_NAME AS CREATED_BY ,CREATED_DATE FROM Issues A, USERS B  Where  A.CREATED_BY=B.USER_ID AND A.ASSIGNED_TO=B.USER_ID AND A.ISSUE_ID=" + ID;
OracleDataAdapter adp = new OracleDataAdapter(selectquery, con);
DataTable dt = new DataTable();
adp.Fill(dt);
//GridView1.DataSource = dt;
// GridView1.DataBind();
lblTitle.Text = dt.Rows[0]["TITLE"].ToString();
lblDescription.Text = dt.Rows[0][1].ToString();
//lblSeverity.Text = dt.Rows[0][2].ToString();
lblStatus.Text = dt.Rows[0][3].ToString();
lblStatus.CssClass ="status"+ dt.Rows[0][3].ToString();
//lblImpact.Text = dt.Rows[0][4].ToString();
lblCreatedby.Text = dt.Rows[0][5].ToString();
lblcreateDate.Text = dt.Rows[0]["CREATED_DATE"].ToString();
//blCommentsDescriptionresult.Text = txtCommentdescription.Text;