Need to append rows in DataGrid from session variable. SQL Database involved.

hhnetworks
hhnetworks used Ask the Experts™
on
In my Web Form, I have a TreeView and a DataGrid. When a user selects a node from the tree, an SQL query retrieves related values from the Database and populates a row in the grid. The user will be selecting multiple nodes, so I need the rows appended to grid, not overwriting previous ones. Also need to be able to delete a row from the grid if the user chooses. I have been cobbling together bits of code from many forums to use session variables, but cant seem to get it to work. As soon as I code anything relating to a session variable the grid stops populating. If I remove the "session stuff" , then the grid works fine, but each row gets overwritten with the next node selection.  My code is below. Thanks in advance for any help. (I am still fairly new to asp.net)

aspx:
 <telerik:RadGrid ID="RadGrid1" runat="server" 
                         
                         AutoGenerateColumns="False"   Skin="Telerik" OnDeleteCommand="RadGrid1_DeleteCommand"  AutoPostBack="true"> 
                        <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
                        <MasterTableView  DataKeyNames="ID" HorizontalAlign="NotSet" AutoGenerateColumns="False">
                        <FooterStyle CssClass="gridFooter"></FooterStyle>
                        <NoRecordsTemplate>
                            Setup / PDI items...
                        </NoRecordsTemplate>
                        <Columns>
                             <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" Visible="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="JobCode" HeaderText="Job Code" UniqueName="JobCode">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Price" HeaderText="Price" DataFormatString="${0}" UniqueName="Price">
                            </telerik:GridBoundColumn>
                            <telerik:GridButtonColumn ConfirmText="Delete this selection?" ConfirmDialogType="RadWindow"
                                ConfirmTitle="Delete" HeaderText="Delete" HeaderStyle-Width="50px"
                                 CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
                                <HeaderStyle Width="50px"></HeaderStyle>
                            </telerik:GridButtonColumn>
                        </Columns>
                      </MasterTableView>
                      </telerik:RadGrid>

Open in new window


cs:
protected void RadTreeView1_NodeCheck(object o, RadTreeNodeEventArgs e)
        {
            RadTreeNode node = e.Node;
            int val = Convert.ToInt32(node.Value);

            #region
            string JobCode = null;
            string Price = null;
            string ID = null;

            dtJC = (DataTable)Session["JobCodeGrid"];

            string selectquery = @"SELECT ID, JobCode, Price FROM Product WHERE ID = @ID";
            SqlCommand cmds = new SqlCommand(selectquery);          
            cmds.CommandType = CommandType.Text;
            cmds.Parameters.AddWithValue("@ID", val);
            con.Open();
            SqlDataReader rdr = cmds.ExecuteReader();
            while (rdr.Read())
            {
                JobCode = rdr["JobCode"].ToString();
                Price = rdr["Price"].ToString();
                ID = rdr["ID"].ToString();
            }
            con.Close();
            DataRow drow = dtJC.NewRow();
            drow[ID] = ID;
            drow[JobCode] = JobCode;
            drow[Price] = Price;
            RadGrid1.DataSource = dtJC;
            RadGrid1.DataBind();
            Session["JobCodeGrid"] = dtJC;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer User Interface
Commented:
Before this line,

RadGrid1.DataSource = dtJC;

add this

dtJC.Rows.Add(drow);

Author

Commented:
Obadiah,

I added the line you recommended, but it has not helped yet. This morning, after debugging my code, Im realizing that the line with  SqlDataReader rdr = cmds.ExecuteReader(); is the last line executed. The 'while' statement and everything below it does not execute. No error messages.
Any ideas? Thanks again.
Obadiah ChristopherDeveloper User Interface

Commented:
Try adding a try catch block. I guess there is an error in your connection to the database.

As per ur code, there is no try catch block.

Author

Commented:
Thanks for your help.  I am closing the question. I did find errors in the construction of my data table, and SQLDataReader statements. Your first reply regarding "dtJC.Rows.Add(drow);" did point me in the right direction. Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial