Southern_Gentleman
asked on
Insert Dropdown, DateSelector, Column from Gridview into SQL Server
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:
My Code Behind:
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>
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER