locked
Unable to retrieve from viewstate RRS feed

  • Question

  • User-1519490041 posted

    Hi All,

    I face the problem which retrieve the data after success insert the date from view state into database.

    here is my insert

    SetRowData();
                        DataTable table = ViewState["CurrentTable"] as DataTable;
    
                        if (table != null)
                        {
                            DataSet ds1 = new DataSet();
    
                            foreach (DataRow row in table.Rows)
                            {
                                Guid itemID = Guid.NewGuid();
                                string strItemNum = row.ItemArray[1] as string;
                                string strItemDes = row.ItemArray[2] as string;
                                string strSupplier = row.ItemArray[3] as string;
    
                                if (strItemNum != null || strItemDes != null)
                                {
    
                                    using (var conn = new SqlConnection(ConnectionString))
                                    {
                                        conn.Open();
                                        string command = "INSERT INTO tableA (Id, Store_SMDFFK, ItemNumber, ItemDes, Supplier) ";
                                        command = command + "VALUES (@Id, @formserial, @itemNum, @itemDes, @supplier)";
    
                                        var cmd = new SqlCommand(command, conn);
                                        cmd.CommandType = System.Data.CommandType.Text;
    
                                        cmd.Parameters.AddWithValue("@Id", itemID);
                                        cmd.Parameters.AddWithValue("@formserial", lblRefNo.Text);
                                        cmd.Parameters.AddWithValue("@itemNum", strItemNum);
                                        cmd.Parameters.AddWithValue("@itemDes", strItemDes);
                                        cmd.Parameters.AddWithValue("@supplier", strSupplier);
                                        cmd.ExecuteNonQuery();
                                    }
                                }
    
                            }
                        }

    Any idea on retrieve data and set it to the view?

    and here is my gridview

    <asp:GridView ID="grvFormDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
                    CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvFormDetails_RowDeleting"
                    Width="97%" Style="text-align: left">
                    <Columns>
                        <asp:BoundField DataField="RowNumber" HeaderText="No." />
                        <asp:TemplateField HeaderText="Item">
                            <ItemTemplate>
                                <table >
                                    <tr>
                                        <td colspan="2" style="font-style: oblique; font-size: x-small">Item Number<asp:TextBox ID="txtItemNumber" runat="server" width="90%"/></td>
                                        <td colspan="2" style="font-style: oblique; font-size: x-small">ItemDes<asp:TextBox ID="txtItemDes" runat="server" width="90%"></asp:TextBox> </td>
                                        <td colspan="2" style="font-style: oblique; font-size: x-small">Supplier<asp:TextBox ID="txtSupplier" runat="server" width="90%"></asp:TextBox></td>
                                    </tr>
                                </table>
                                <hr />
                            </ItemTemplate>
    
                            <FooterStyle HorizontalAlign="Right" />
                            <FooterTemplate>
                                <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                            </FooterTemplate>
                        </asp:TemplateField>
                         <asp:CommandField ShowDeleteButton="True" />
                       
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#EFF3FB" />
                    <EditRowStyle BackColor="#2461BF" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>


    Here is the problem, when i retrieve the data from database, i unable to bind it to gridview.

    private void LoadAllItem(string abcd)
            {
     
                using (var conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tableA WHERE id= '"+abcd+"'", conn);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
    
                    DataRow dr = dt.NewRow();
                    dt.Rows.InsertAt(dr, 0);
                    //Creating the first row of GridView to be Editable
                    grvFormDetails.EditIndex = 0;
                    grvFormDetails.DataSource = dt;
                    grvFormDetails.DataBind();
               }
    }

    Wednesday, December 31, 2014 2:43 AM

All replies

  • User1577371250 posted

    what is the problem here?

    Wednesday, December 31, 2014 2:55 AM
  • User2008642861 posted

    Hi Renne Hong,

    From the post, it is hard for me to reproduce your issue. I am not clear about how do you trigger your function of insert date into database. I suppose that your function of  private

    void LoadAllItem(string abcd) may conflict with some other function.

    I suggest that please add a breakpoint at LoadAllItem() to check if the function has read from data base. Or you could provide us more details of your code which we could reproduce your issue, I would be happy to help you out in the best possible way.

    Best regards,

    Archer Wang

    Thursday, January 1, 2015 10:20 PM