如何将数据表列id更改为int(类型转换)

本文关键字:int 类型转换 数据表 id | 更新日期: 2023-09-27 18:03:37

数据表:

 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());

那么如何将数据表列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");
        }
    }

我想要更新或插入数据到数据库..

问题是,虽然Int32 dtemps得到零显示输入字符串不是在正确的格式..

如何将数据表列id更改为int(类型转换)

尝试克隆数据表,然后更改列的类型,如

DataTable mydt = dtemp.Clone();
mydt.Columns[0].DataType = typeof(Int32);
foreach (DataRow row in dtemp.Rows) 
{
    mydt.ImportRow(row);
}

你的DataTable可能是空的,这就是为什么它不能工作。

检查行i小于RowCount,例如:

if (dtempcount.Rows.Count > i)