Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My design Page:

XML
<<asp:GridView ID="GridView1" runat="server"
                        AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE"
                        BorderStyle="None" BorderWidth="1px" CellPadding="4"
                        OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ForeColor="Black"
                        GridLines="Vertical" onrowdatabound="GridView1_RowDataBound1"
                        Width="472px">
<Columns>
<asp:TemplateField HeaderText="Encrypt Msg">


<ItemTemplate>
<asp:TextBox ID="TextBox1"  runat="server" Width="200px" TextMode="MultiLine" Text='<%# Eval("Split") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Storage Server">


<ItemTemplate>

<asp:DropDownList ID="Eligible" runat="server" Width="193px">

<asp:ListItem >Storage Server1</asp:ListItem>

<asp:ListItem >Storage Server2</asp:ListItem>

<asp:ListItem >Storage Server3</asp:ListItem>

</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
</asp:GridView>
                    &nbsp;<br />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td style="width: 374px">
                    <asp:Button ID="upload" runat="server" BorderStyle="None"
                        onclick="Button1_Click" Text="Click to Upload " />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>



in upload.cs:

C#
protected void upload_Click(object sender, EventArgs e)
{

      for (int j = 0; j < GridView1.Rows.Count; j++)
        {

            //con.Open();
                SqlCommand comm = new SqlCommand("insert into sender '" + GridView1.Rows[j].Cells[0].Text + "','" + GridView1.Rows[j].Cells[1].Text + "'", con);
                if (con.State.Equals(ConnectionState.Open))
                {
                    comm.ExecuteNonQuery();
                }
            }


}




i want to insert all gridview dropdownlist value and textbox vlaue to table...pls help me...
Posted
Updated 26-Feb-13 3:11am
v3
Comments
Nandakishore G N 26-Feb-13 9:06am    
what have you done in codebehind paste it?
joshrduncan2012 26-Feb-13 9:22am    
Gotta put sql injection prevention measures in place, too. Your code is susceptible for sql injection attacks.
Deenuji 26-Feb-13 9:30am    
how to put that prevention ????

C#
foreach(GridViewRow gvr in GridView1.Rows)
    {
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            string value = ((DropDownList)gvr.FindControl("didDdl")).SelectedValue;

           //here write code for inserting in sql table.use parameterized query for preventing from sql injection.

        }
    }


see example of parameterized query in below link

http://stackoverflow.com/questions/12301776/problems-inserting-to-sql-table[^]

Thanks
 
Share this answer
 
Comments
Deenuji 27-Feb-13 4:55am    
getting errors only sir....
Deenuji 27-Feb-13 5:16am    
got output but it doesnt return dropdown select item value .......it returns only storage server 1.........
AshishChaudha 27-Feb-13 23:07pm    
what error you are getting..paste your code.
Deenuji 27-Feb-13 23:41pm    
i didnt getting error now sir....but it doesn't return the dropdown selected value......it returns only dropdown(ex...storage server1) first value....
AshishChaudha 28-Feb-13 4:36am    
first value means the first item of dropdownlist?? its not returning the selected value??
C#
protected void upload_Click(object sender, EventArgs e)
{

      for (int j = 0; j < GridView1.Rows.Count; j++)
        {

TextBox TextBox1 =(TextBox)GridView1.Row[j].FindControl("TextBox1 ");
DropDownList Eligible=(DropWownList)GridView1.Row[j].FindControl("Eligible");

            //con.Open();
                SqlCommand comm = new SqlCommand("insert into sender '" + TextBox1 .Text + "','" + Eligible.Item[Eligible.SelectedIndex].Value + "'", con);
                if (con.State.Equals(ConnectionState.Open))
                {
                    comm.ExecuteNonQuery();
                }
            }


}
 
Share this answer
 
Comments
Deenuji 26-Feb-13 9:48am    
getting error on this code...sorry

Errors:
Error1: Unrecognized '@' block type
Error2: 'System.Web.UI.WebControls.GridView' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.GridView' could be found (are you missing a using directive or an assembly reference?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900