【问题标题】:RowUpdate event not actually Upadating Database行更新事件实际上并未更新数据库
【发布时间】:2012-09-17 19:23:34
【问题描述】:

我的 Gridview 是一种让我查看数据库表并在需要时执行编辑和更新的方法

数据实际上由 moth 、 year 和 DaysPerMonth (可工作天数)组成

所有字段都是Int的类型

所以显示当前月份而不是我正在使用的给定月份的 int 值/格式 双向翻译,数据库外 - 在gridview中显示为月份名称(字符串)

   public static CultureInfo ILci = CultureInfo.CreateSpecificCulture("he-IL");

   public static string GetMonthName(int mInt, int mYear=2012)
   {
        DateTime fullDate = new DateTime(mYear, mInt, 2);
        string[] tempDayArray = fullDate.ToString("MMMM", ILci).Split(' ');
        return ILci.DateTimeFormat.GetMonthName(mInt);

    }

并通过“翻译器”/格式化程序返回数据库

    public static int GetMonthAsInt(string mStr)
    {
        return DateTime.ParseExact(mStr, "MMMM", ILci).Month;

    }

从一开始所有阶段都可以 - 查看模式 通过编辑 - 编辑模式 即使在更新模式下,

我可以用 VS 调试器检查 SQL 数据源更新命令的值实际上是“Good To Go” 并在 SQL 服务器查询中执行它的命令 返回一个成功的结果。

所以问题仍然存在于更新结束或更新后 DataBind(); 线在最后 我得到的错误/异常是:

输入字符串的格式不正确。

这是 GV_DaysPerMonth_RowUpdating 事件处理程序代码

    protected void GV_DaysPerMonth_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        // by recived i ment what is taken by sql command from gridview

        string recivedNameMonth=string.Empty;
        int recivedYears = 0;
        int recivedDprM = 0;
        int RowNo = 0;

        GridViewRow CurRow = GV_DaysPerMonth.Rows[e.RowIndex];
        string[] formValues = new string[CurRow.Cells.Count-1];
        bool note = ((TextBox)(CurRow.Cells[0].Controls[1])).Text != null;
        if (note)
        {
            for (int i = 0; i < CurRow.Cells.Count-1; i++)
            {
                formValues[i] = ((TextBox)(CurRow.Cells[i].Controls[1])).Text;
            }
            //recivedNameMonth = ((TextBox)(CurRow.Cells[0].Controls[1])).Text;
        }

        RowNo = e.RowIndex + 1;

        recivedNameMonth = formValues[0];
        int Montint = RobCS.RDates.GetMonthAsInt(recivedNameMonth);
        recivedYears = Convert.ToInt32(formValues[1]);
        recivedDprM = Convert.ToInt32(formValues[2]);

        dsWorkDayPerMonth.UpdateCommand = "UPDATE [tblWorkDaysPerMonth] SET [theMonth] = " + Montint+ ", [theYear] = "+recivedYears+",[WorkDaysPerMonth] = " + recivedDprM + " WHERE [recordID] = " + RowNo;

        GV_DaysPerMonth.DataBind();

    } 

使用它

SqlDataSource

<asp:sqldatasource runat="server" id="dsWorkDayPerMonth" 
ConnectionString="Data Source=(local);Initial Catalog=hental;Integrated Security=True" 
ProviderName="System.Data.SqlClient" 
SelectCommand="SELECT * FROM [tblWorkDaysPerMonth]"
UpdateCommand="UPDATE [tblWorkDaysPerMonth] SET [theMonth] = @theMonth, [theYear] = @theYear, 
                                    [WorkDaysPerMonth] = @WorkDaysPerMonth WHERE [recordID] = @recordID" 
DeleteCommand="DELETE FROM [tblWorkDaysPerMonth] WHERE [recordID] = @recordID" 
InsertCommand="INSERT INTO [tblWorkDaysPerMonth]">
<UpdateParameters> 
    <asp:Parameter Name="theMonth" Type="Int32" /> 
    <asp:Parameter Name="theYear" Type="Int32" /> 
    <asp:Parameter Name="WorkDaysPerMonth" Type="Int32" /> 
    <asp:Parameter Name="recordID" Type="Int32" /> 
</UpdateParameters> 
<DeleteParameters> 
    <asp:Parameter Name="recordID" Type="Int32" /> 
</DeleteParameters> 
</asp:sqldatasource>

使用 GridView

                <asp:TemplateField HeaderText="Month" ControlStyle-Width="100" HeaderStyle-Width="120" ItemStyle-HorizontalAlign="Center"> 
                    <ItemTemplate> 
                        <%# Eval("theMonth")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="TBX_theMonth" runat="server" Text='<%# Bind("theMonth")%>' /> 
                    </EditItemTemplate> 

【问题讨论】:

  • 如果我是你,我会查看你的 formValues 数组的内容并验证它们是否可以解析为整数。
  • 他们都做了,问题是它自己的实际更新的语法/过程。在“执行”更新之前检查我从每个变量 /sql 参数收集的数据值......我确实把它放在引号中,因为我认为我更新数据库的方法是错误的
  • 我在这里超出了我的专业领域,但我相信您不需要更改UpdateCommand 的文本。它已经参数化了。那么问题将是如何向 UpdateCommand 提供参数,而我不知道。 (我不使用 SqlDataSource。)

标签: c# asp.net sql-server-2008 gridview


【解决方案1】:

因为它是在没有要求的第一个代码示例中实现的

       //your connection string here 

        SqlConnection con = RobCS_109.RDB.HentlConn;

        con.Open();
        string updateCMD = dsWorkDayPerMonth.UpdateCommand;
        DataSet ds = new DataSet();
        SqlDataAdapter da;

        //use a select command and SqlConnection
        da = new SqlDataAdapter(dsWorkDayPerMonth.SelectCommand, con);

        //same here i have used all values recived in the example(top one)code
        // event -RowUpdating above, then i am using
       //stored sqlUpdate string for the command below
        da.UpdateCommand = new SqlCommand(SQLUP);
        da.Fill(ds, "tblWorkDayPerMonth");


        dsWorkDayPerMonth.Update();


        con.Close();

现在它确实起作用了,如果您在更新事件时遇到问题,您可以在这里尝试 稍后我将通过using statemet 进一步尝试改进它,我会尽快了解如何

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 1970-01-01
    • 2012-04-09
    • 2019-08-16
    • 2010-10-19
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多