0

我的 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 调试器检查实际上是“Good To Go”的 SQL 数据源更新命令的值,并在 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> 

4

1 回答 1

0

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

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

现在它确实起作用了,如果您在更新事件时遇到问题,您可以在这里尝试,稍后我将进一步尝试通过usingstatemet 改进它,一旦我将学习如何

于 2012-09-19T05:46:18.317 回答