Solved

Insert Dropdown, DateSelector, Column from Gridview into SQL Server

Posted on 2014-11-17
2
70 Views
Last Modified: 2015-07-29
I previously asked a question regarding a date field in a gridview to be inserted into my sql server. I realized that that the date was being selected from a dateselector, the next column is a dropdown selector field, the third is a value from my select query. I can insert the data if it were populated from the select statement but i'm having trouble inserting the dateselector and the dropdown box value from the gridview. My markup seems to fine since I'm able to view the gridview with the objects but can't seem to insert it in my SQL server in the code behind. Any help would be appreciated.

My Markup:

<asp:GridView ID="gvPrint" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="gvAll" Font-Names="Calibri" Font-Size="X-Small" Width="100%">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                <asp:CheckBox ID="chk" runat="server" />
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Print Date">
                            <ItemTemplate>
                                <dx:ASPxDateEdit ID="ASPxDateEdit1" runat="server" Theme="DevEx" Width="80px" AutoPostBack="true">
                                </dx:ASPxDateEdit>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Printer">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sqlPrinters" DataTextField="Printer" DataValueField="Printer" AutoPostBack="true">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="sqlPrinters" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [Printer] FROM [Printer]" ></asp:SqlDataSource>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" >
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>

Open in new window


My Code Behind:

Protected Sub btnSchedule_Click(sender As Object, e As EventArgs) Handles btnSchedule.Click
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("PrintDate", GetType(DateTime)), New DataColumn("Printer", GetType(String)), New DataColumn("OrderID", GetType(Integer))})
        For Each Row As GridViewRow In gvPrint.Rows
            If TryCast(Row.FindControl("chk"), CheckBox).Checked Then
                Dim PrintDate As DateTime
                Dim DateValid As [Boolean] = DateTime.TryParse(Row.Cells(1).Text, PrintDate)
                Dim Printer As String = Row.Cells(2).Text
                Dim OrderID As Integer = Integer.Parse(Row.Cells(3).Text)
                dt.Rows.Add(PrintDate, Printer, OrderID)
            End If
        Next
        If dt.Rows.Count > 0 Then
            Dim conString As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
            Using con As New SqlConnection(conString)

                Using BulkCopy As New SqlBulkCopy(con)

                    BulkCopy.DestinationTableName = "dbo.PrinterSchedule"

                    BulkCopy.ColumnMappings.Add("PrintDate", "PrintDate")
                    BulkCopy.ColumnMappings.Add("Printer", "Printer")
                    BulkCopy.ColumnMappings.Add("OrderID", "OrderID")
                    con.Open()
                    BulkCopy.WriteToServer(dt)
                    con.Close()
                End Using
            End Using
        End If
    End Sub

Open in new window

0
Comment
Question by:Southern_Gentleman
2 Comments
 
LVL 4

Accepted Solution

by:
ItWorked earned 500 total points
ID: 40449516
Are you talking about example as of such? Here
0
 

Author Comment

by:Southern_Gentleman
ID: 40456430
Sort of what I was looking for. I'm looking at taking three columns from my gridview and inserting it in a table that I created. The first column has checkboxes. This selects which rows to be bulk inserted into my table. The second column has a date select. the third is a dropdown box. the forth column is the order number. On a click event button, any checkbox selected will have the dateselect value, dropdown box value and ordered inserted into my table. I'm able to Insert the order number since that is from my select statement. I just can't seem to insert the values from my dateselect and dropdown box. So i'm not really wanting to update or edit any information from my gridview. I'm just looking to insert it into my table.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question