行更新事件实际上不是更新数据库

时间:2012-09-17 19:23:34

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

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

数据实际上是由飞蛾,年和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);

    }

并通过“translator”/ formatter

返回数据库
    public static int GetMonthAsInt(string mStr)
    {
        return DateTime.ParseExact(mStr, "MMMM", ILci).Month;

    }

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

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

因此问题仍然存在于更新结束时或更新后的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> 

                     

1 个答案:

答案 0 :(得分:0)

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

       //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状态进行改进,一旦我将学习如何