如何将gridview数据插入数据库 [英] How to insert gridview data into the database

查看:103
本文介绍了如何将gridview数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的错误输入字符串格式不正确



我尝试了什么:



i'm getting the error input string was not in correct format

What I have tried:

try
{
    if (ViewState["dtAck"] != null)
        dtAck = ViewState["dtAck"] as DataTable;

    if (dtAck == null)
        dtAck = AddPanchayats();
    string strFile = string.Empty;
    if (dtAck.Rows.Count > 0)
    {
        //Saving the data
        DataSet dataSetPanchayats = new DataSet();
        dataSetPanchayats.Tables.Add(dtAck);
        string fName = string.Empty;

        foreach (GridViewRow gvr in gvhouseholddetials.Rows)
        {
            TextBox i = (TextBox)gvr.FindControl("txtNOOFCARDSDISTRIBUTED");
            CheckBox checkBox = (CheckBox)gvr.FindControl("checkBox");
            int totalJC = Convert.ToInt32(gvhouseholddetials.Rows[gvr.RowIndex].Cells[4].Text.Trim());
            if (Convert.ToInt32(i.Text) > totalJC && checkBox.Checked)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Warning", "alert('Please enter proper value of Total no of cards distributed');", true);
                //gvhouseholddetials.Rows[gvr.RowIndex].Focus();
                TextBox txtNOOFCARDSDISTRIBUTED = gvhouseholddetials.Rows[gvr.RowIndex].FindControl("txtNOOFCARDSDISTRIBUTED") as TextBox;
                txtNOOFCARDSDISTRIBUTED.Focus();
                txtNOOFCARDSDISTRIBUTED.Enabled = true;
                checkBox.Checked = false;
                return;
            }

            TextBox d = (TextBox)gvr.FindControl("txtD");
            string txtdate = Convert.ToString(d.Text);

            if (txtdate == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Warning", "alert('Please select distributed date');", true);
                gvhouseholddetials.Rows[gvr.RowIndex].Focus();
                gvhouseholddetials.Rows[gvr.RowIndex].Cells[5].Focus();
                gvhouseholddetials.Rows[gvr.RowIndex].Cells[5].Enabled = true;

                TextBox txtTD = gvhouseholddetials.Rows[gvr.RowIndex].FindControl("txtD") as TextBox;
                txtTD.Enabled = true;
                checkBox.Checked = false;
                return;
            }
            if (txtdate != "" && checkBox.Checked)
            {
                DateTime myDate = DateTime.ParseExact(txtdate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                DateTime startDate = DateTime.ParseExact("01/01/2017", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                if (myDate > DateTime.Today || myDate < startDate)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Warning", "alert('Please enter proper date' );", true);
                    gvhouseholddetials.Rows[gvr.RowIndex].Focus();
                    gvhouseholddetials.Rows[gvr.RowIndex].Cells[5].Focus();
                    gvhouseholddetials.Rows[gvr.RowIndex].Cells[5].Enabled = true;

                    TextBox txtTD = gvhouseholddetials.Rows[gvr.RowIndex].FindControl("txtD") as TextBox;

                    txtTD.Enabled = true;
                    checkBox.Checked = false;
                    return;
                }
            }


        }


        string msg = new GenericBO().GetString(new object[] { dataSetPanchayats.GetXml(), objPortalUser.PCCInfoCode, objPortalUser.UserName, null }, "JobCard_Printing.SaveAcknowledgedJobCard_APO");
        if (msg == "SUCCESS")
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Warning", "alert('Data saved successfully');", true);
            ViewState.Remove("dtAck");
            gvhouseholddetials.DataBind();
            ViewState.Remove("bindGrid");
            //btnSubmit.Visible = false;
        }

推荐答案

您正在使用 Convert.Int32 DateTime.ParseExact 代码中的方法。这些可能导致例外。调试这两个方法调用,您可能会发现问题。



您可能正在尝试将非数字转换为数字或不同格式的日期转换为日期对象。
You are using Convert.Int32 and DateTime.ParseExact methods in your code. These can lead to exceptions. Debug these two method calls and you might find the problem.

You are probably trying to convert a non-number to a number or a date in different format to a date object.


这篇关于如何将gridview数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆