Friday, 9 August 2013

INSERT VALUES FROM GRIDPANEL TO DATABASE ON BUTTON CLICK IN EXT.NET






The code given below is to perform Gridpanel insertion in EXT.NET.
First in the aspx page create a gridpanel like this.


<ext:GridPanel
            ID="GridPanel1"
            runat="server"
            StoreID="Store1"
            Collapsible="true"
            Cls="x-grid-custom"
            ForceFit="true">
            <ColumnModel ID="ColumnModel1" runat="server">
           <Columns>
                    <ext:Column ID="Column1" runat="server" Text="Monday" DataIndex="Monday" Width="75" />
                    <ext:Column ID="Column2" runat="server" Text="Tuesday" Width="75" DataIndex="Tuesday"/>
                    <ext:Column ID="Column3" runat="server" Text="Wednesday" Width="75" DataIndex="Wednesday" />
                    <ext:Column ID="Column4" runat="server" Text="Thursday" Width="75" DataIndex="Thursday" />
                     <ext:Column ID="Column5" runat="server" Text="Friday" Width="75" DataIndex="Friday" />
                     <ext:Column ID="Column6" runat="server" Text="Saturday" Width="75" DataIndex="Saturday" />
           </Columns>
            </ColumnModel>
                   
            <Buttons>
               <ext:Button ID="Button1" runat="server" Text="Reset">
                    <DirectEvents>
                        <Click OnEvent="Reset">
                        </Click>
                    </DirectEvents>
                </ext:Button>          
                <ext:Button ID="Button2" runat="server" Text="Submit">
                    <DirectEvents>
                        <Click OnEvent="SubmitSelection">
                            <ExtraParams>
                                <ext:Parameter Name="Values" Value="Ext.encode(#{GridPanel1}.getRowsValues({selectedOnly:false}))" Mode="Raw" />                            </ExtraParams>
                        </Click>
                    </DirectEvents>
                </ext:Button>
           
             
            </Buttons>
        </ext:GridPanel>
         <ext:Label ID="Label1" runat="server" Hidden="true" /><br />
         <ext:Label ID="Label2" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label3" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label4" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label5" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label6" runat="server" Hidden="true"/>

         

The labels are temporary storage locations and therefore you have to give the same number of Labels as that of the columns.Here I use 6 Labels since I had 6 columns.

Now at the code behind section give the following code.




     protected void SubmitSelection(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["Values"];
            Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);
            foreach (Dictionary<string, string> row in companies)
            {
                int i = 1;
                StringBuilder sb = new StringBuilder();
                StringBuilder sb1 = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                StringBuilder sb3 = new StringBuilder();
                StringBuilder sb4 = new StringBuilder();
                StringBuilder sb5 = new StringBuilder();
                foreach (KeyValuePair<string, string> keyValuePair in row)
                {
                    if (i == 1)
                    {
                        sb.Append(keyValuePair.Value);
                    }
                    else if (i == 2)
                        sb1.Append(keyValuePair.Value);
                    else if (i == 3)
                        sb2.Append(keyValuePair.Value);
                    else if (i == 4)
                        sb3.Append(keyValuePair.Value);
                    else if (i == 5)
                        sb4.Append(keyValuePair.Value);
                    else if(i == 6)
                        sb5.Append(keyValuePair.Value);
                    i++;
                }
                this.Label1.Text = sb.ToString();
                this.Label2.Text = sb1.ToString();
                this.Label3.Text = sb2.ToString();
                this.Label4.Text = sb3.ToString();
                this.Label5.Text = sb4.ToString();
                this.Label6.Text = sb5.ToString();
                SqlConnection con = new SqlConnection("YOUR CONNECTIONSTRING");
                string query = "insert into tablename 0values ('" + Label1.Text + "','" + Label2.Text + "','" + Label3.Text + "','" + Label4.Text + "','" + Label5.Text + "','" + Label6.Text + "')";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



In this the 'i' value represents the number of columns.

0 comments:

Post a Comment