我正在创建一个网页,用户可以在其中编辑DataGrid
中的信息,但我一直收到此错误:
异常详细信息:System.IndexOutOfRangeException:没有行 位置0。
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
Label lblProj = this.Master.FindControl("lblProj") as Label;
Label lblStr = this.Master.FindControl("lblStruct") as Label;
if (Session["projCode"] == null && Session["structCode"] == null)
{
Response.Redirect("frmSignIn.aspx");
}
else
{
FillGrid();
lblProj.Text = cbal.getField("Project_View", "@PrjM_id_int", Session["projCode"].ToString(), "NameByCode");
string[,] paramVal = new string[2, 2];
paramVal[0, 0] = "@sd_Code_var";
paramVal[0, 1] = Session["structCode"].ToString();
paramVal[1, 0] = "@Sd_ProjectCode_var";
paramVal[1, 1] = Session["projCode"].ToString();
DataSet ds = cbal.fillGrid_with_param("Structure_Definition_GetStrctureName", paramVal);
lblStr.Text = ds.Tables[0].Rows[0]["STRUCTURE NAME"].ToString();
}
if (!IsPostBack)
{
FillContQuot();
FillContractor();
}
// drpContractorName.SelectedIndexChanged += new EventHandler(drpContractorName_SelectedIndexChanged);
}
protected void drpContractorName_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CQRA"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from tbl_Contractor_Master where CNT_Code_var =' " + drpContractorName.SelectedValue.ToString() + " '", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
int count = 0;
Label1.Visible = true;
while (dr.Read())
{
count++;
}
if (count > 0)
{
Label1.Text = "This Contractor Not Available";
}
else
{
Label1.Text = "This Contractor Already Exist";
}
//Label1.Visible = false;
}
private void FillContractor()
{
string[,] paramVal2 = new string[1, 2];
paramVal2[0, 0] = "@CNT_Id_int";
paramVal2[0, 1] = "-1";
DataSet ds = cbal.fillGrid_with_param("Contractor_Master_view", paramVal2);
Dictionary<string, string> dct = new Dictionary<string, string>();
dct.Add("0", "Select");
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dct.Add(ds.Tables[0].Rows[i]["CONTRACTOR CODE"].ToString(), ds.Tables[0].Rows[i]["CONTRACTOR NAME"].ToString());
}
}
drpContractorName.DataSource = dct;
drpContractorName.DataValueField = "Key";
drpContractorName.DataTextField = "Value";
drpContractorName.DataBind();
}
private void FillContQuot()
{
string[,] paramVal2 = new string[1, 2];
paramVal2[0, 0] = "@CQ_ID_int";
paramVal2[0, 1] = "-1";
DataSet ds = cbal.fillGrid_with_param("contract_Quotaionapply", paramVal2);
Dictionary<string, string> dct = new Dictionary<string, string>();
dct.Add("0", "Select");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dct.Add(ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][0].ToString());
}
drpConQuot.DataSource = dct;
drpConQuot.DataValueField = "Key";
drpConQuot.DataTextField = "Value";
drpConQuot.DataBind();
}
protected void DrpConQuotSelectedIndexChanged(object sender, EventArgs e)
{
lblMsg.Text = "";
if (drpConQuot.SelectedIndex != 0)
{
FillGrid();
btnsave.Visible = true;
}
else
{
grdQuotation.DataSource = null;
grdQuotation.DataBind();
}
}
protected void DrpContractorNameSelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCQRA@12Sept2012TGConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from tbl_Contractor_Master where CNT_Code_var =' " + drpContractorName.SelectedValue.ToString() + " '", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
int count = 0;
Label1.Visible = true;
while (dr.Read())
{
count++;
}
if (count > 0)
{
Label1.Text = "Not Available";
}
else
{
Label1.Text = "Available";
}
}
private void FillGrid()
{
string[,] param = new string[1,2];
Dictionary<string, string> key = new Dictionary<string, string>();
//item.Clear();
key.Add("0", "--Select--");
param[0, 0] = "@Quotation_code_Var";
try
{
param[0, 1] = drpConQuot.SelectedItem.Value;
}
catch
{
param[0, 1] = "";
}
dsQuot = cbal.fillGrid_with_param("Quotation_Comparision", param);
if (dsQuot.Tables.Count <= 0)
return;
DataTable tab = new DataTable();
tab.Columns.Add("ITEM NAME");
for (int i = 0; i < dsQuot.Tables.Count; i++)
{
try
{
tab.Columns.Add(dsQuot.Tables[i].Rows[0]["Contractor"].ToString().Split('/')[0]);
}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();
}
}
foreach (DataRow r1 in dsQuot.Tables[0].Rows)
{
DataRow r = tab.NewRow();
//for (int i = 0; i < r1.ItemArray.Length - 1; i++)
r[0] = r1[0].ToString();
tab.Rows.Add(r);
}
for (int i = 0; i < dsQuot.Tables.Count; i++)
{
int c = 0;
foreach (DataRow r1 in dsQuot.Tables[i].Rows)
{
tab.Rows[c][i+1] = r1[1].ToString();
c++;
}
}
grdQuotation.DataSource = tab;
grdQuotation.DataBind();
}
protected void Btnsaveclick(object sender, EventArgs e)
{
try
{
string cotationno = drpConQuot.SelectedValue;
string quot_AwardedTo = drpContractorName.SelectedValue;//(r.Cells[r.Cells.Count - 2].FindControl("ddlName") as DropDownList).SelectedValue;
string[,] para = new string[2, 2];
para[0, 0] = "@quot_no_int";
para[1, 0] = "@quot_AwardedTo";
para[0, 1] = cotationno;
para[1, 1] = quot_AwardedTo;
cbal.fillGrid_with_param("Contract_AwardedTo", para);
lblMsg.Text = "Records Saved!";
}
catch(Exception e1)
{
lblMsg.Text = e1.Message;
}
}
protected void GridView1RowCreated(object sender, GridViewRowEventArgs e)
{
}
答案 0 :(得分:2)
检查您的DataSet是否有行。
if(DataSet.Tables[0].rows.Count > 0 )
{
// your Code
}
答案 1 :(得分:0)
当您使用DataSet
时,需要使用支票ds.table[0].rows.count > 0
而不是您需要的代码
DataSet ds = cbal.fillGrid_with_param("Contractor_Master_view", paramVal2);