这是数据表:
DataTable dtemp = new DataTable();
dtemp = objDB.SelectEmployeeSalaryByYear11(Convert.ToInt32(ddlyear.SelectedValue));
我正在尝试这个,但显示问题..
Int32 dtemps = int.Parse(dtemp.Rows[i]["ID"].ToString());
and
Int32 dtemps = Convert.ToInt32(dtemp.Rows[i]["ID"].ToString());
那么如何将datatatble列id转换为int?
是完整的代码:
protected void btnSave_Click(object sender, EventArgs e)
{
string result = string.Empty;
string upld = string.Empty;
int i = 0;
try
{
foreach (GridViewRow gvr in grdSalary.Rows)
{
DataTable dtemp = new DataTable();
dtemp = objDB.SelectEmployeeSalaryByYear11(Convert.ToInt32(ddlyear.SelectedValue));
Int32 dtemps = Convert.ToInt32(dtemp.Rows[i]["ID"].ToString());
Int32 empId = Convert.ToInt32(grdSalary.DataKeys[gvr.RowIndex]["EID"]);
DataTable dtempcount = new DataTable();
dtempcount = objDB.SelectEmployeeSalaryByEmpIDYear11(Convert.ToInt32(ddlyear.SelectedValue), empId);
TextBox txtjan = gvr.FindControl("txtjan") as TextBox;
TextBox txtfeb = gvr.FindControl("txtfeb") as TextBox;
TextBox txtmarch = gvr.FindControl("txtmarch") as TextBox;
TextBox txtapril = gvr.FindControl("txtapril") as TextBox;
TextBox txtmay = gvr.FindControl("txtmay") as TextBox;
TextBox txtjune = gvr.FindControl("txtjune") as TextBox;
TextBox txtjuly = gvr.FindControl("txtjuly") as TextBox;
TextBox txtaug = gvr.FindControl("txtaug") as TextBox;
TextBox txtsept = gvr.FindControl("txtsept") as TextBox;
TextBox txtoct = gvr.FindControl("txtoct") as TextBox;
TextBox txtnov = gvr.FindControl("txtnov") as TextBox;
TextBox txtdec = gvr.FindControl("txtdec") as TextBox;
if (dtempcount.Rows.Count == 0)
{
Int32 Intuserid = objDB.insert_updateSalary(0, Convert.ToInt32(grdSalary.DataKeys[gvr.RowIndex]["EID"]), Convert.ToInt32(ddlyear.SelectedValue),
txtjan.Text, txtfeb.Text, txtmarch.Text, txtapril.Text, txtmay.Text, txtjune.Text, txtjuly.Text, txtaug.Text,
txtsept.Text, txtoct.Text, txtnov.Text, txtdec.Text);
}
else
{
try
{
// Int32 Intuserid = objDB.insert_updateSalary(Convert.ToInt32(dtemp.Rows[i]["ID"]), Convert.ToInt32(grdSalary.DataKeys[gvr.RowIndex]["EID"]), Convert.ToInt32(ddlyear.SelectedValue),
//txtjan.Text, txtfeb.Text, txtmarch.Text, txtapril.Text, txtmay.Text, txtjune.Text, txtjuly.Text, txtaug.Text,
//txtsept.Text, txtoct.Text, txtnov.Text, txtdec.Text);
Int32 Intuserid = objDB.insert_updateSalary(int.Parse(dtemp.Rows[i]["ID"].ToString()), Convert.ToInt32(grdSalary.DataKeys[gvr.RowIndex]["EID"]), Convert.ToInt32(ddlyear.SelectedValue),
txtjan.Text, txtfeb.Text, txtmarch.Text, txtapril.Text, txtmay.Text, txtjune.Text, txtjuly.Text, txtaug.Text,
txtsept.Text, txtoct.Text, txtnov.Text, txtdec.Text);
}
catch (Exception ex)
{
throw ex;
}
}
i++;
}
}
catch (Exception ex)
{
Response.Redirect("Error.aspx");
}
}
我想更新或插入数据到DATATABSE ..
问题是当Int32 dtemps变为零时显示输入字符串的格式不正确..
答案 0 :(得分:0)
尝试克隆数据表,然后更改列的类型,如
DataTable mydt = dtemp.Clone();
mydt.Columns[0].DataType = typeof(Int32);
foreach (DataRow row in dtemp.Rows)
{
mydt.ImportRow(row);
}
答案 1 :(得分:0)
您的DataTable可能是空的,这就是它无法正常工作的原因。
Check Row i
小于RowCount,例如:
if (dtempcount.Rows.Count > i)