How can i take the TaskEventArgs from a control - asp.net

Am using Telerik RadGantt chart. i need to insert the event to the data base for that am using the code as given:
Private Sub RadGantt1_TaskUpdate(ByVal sender As Object, ByVal e As Telerik.Web.UI.Gantt.TaskEventArgs) Handles RadGantt1.TaskUpdate
mssql = "insert into project " & _
" (ParentID, OrderID, Title, Start, End, PercentComplete, Expanded, Summary)" & _
" values('E1','" & e.Tasks.GetEnumerator.Current.ID & "', " & _
" '" & e.Tasks.GetEnumerator.Current.Title & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Start & "'," & _
" '" & e.Tasks.GetEnumerator.Current.End & "'," & _
" '" & e.Tasks.GetEnumerator.Current.PercentComplete & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Expanded & "'," & _
" '" & e.Tasks.GetEnumerator.Current.Summary & "')"
Dim mycommand As OdbcCommand
mycommand = New OdbcCommand(mssql, dbcon)
dbcon.Open()
Dim mnresult As Integer = mycommand.ExecuteNonQuery()
If mnresult = 1 Then
EventAdd = False
End If
End Sub
But it gives an object reference error in e.Tasks.GetEnumerator.Current.ID. then how can i take the value from this event?

e.Tasks is a collection so you should iterate through it (e.g., in a for..each loop).
To get a single tasks whose properties you can read you should use the enumerator of the collection, for example e.Tasks(1).GetEnumerator.Current.ID

Related

How do I prevent Application.Match Function giving me a Run-Time Error 91?

I'm trying to find a match in a column, return the range of that match, then display a message box with values derived from cells offset from that match's range. But I get error code 91 when I run this. I'm not a great coder, so any extra details you can provide would be really appreciated, as sometimes I struggle to understand the answers provided if it's too jargony.
I was using the find function, but occasionally, the match I want to find is hidden via filters in the spreadsheet, and I read somewhere that the match function won't be affected by whether the cells are filtered or not.
Sub GetInfo2()
Dim Item As String
Dim FindRng As Range
Item = InputBox("What is the item number?", "Item Number")
FindRng = Application.Match(Item, Worksheets("Current Week Summary").Columns(1), 0)
If Not FindRng Is Nothing Then
MsgBox ("Description: " & FindRng.Offset(0, 4).Value & vbCrLf & _
"Flyer/FEM: " & FindRng.Offset(0, 1).Value & vbCrLf & _
"Margin Maker: " & FindRng.Offset(0, 2).Value & vbCrLf & _
"ISR Week: " & FindRng.Offset(0, 3).Value & vbCrLf & _
"Amount To Sell: " & Round(FindRng.Offset(0, 7).Value, 0) & vbCrLf & _
"Cost: $" & FindRng.Offset(0, 18).Value & vbCrLf & _
"Months of Supply: " & Round(FindRng.Offset(0, 35).Value, 0) & vbCrLf & _
"E&O Qty: " & FindRng.Offset(0, 17)), vbOKOnly, Item
End If
End Sub
I did overcome the issue of not being able to search in filtered rows. I ended up using the VLookup Function to find my matches. It apparently searches whether it's filtered or not. I'm still stumped on how to use the match function, so I'd love to know how to do that. But this solved my problem in the interim.
Sub GetInfo()
Dim Item As String
Dim Description As String
Dim FlyerFEM As String
Dim MargMak As String
Dim ISR As String
Dim ATS As String
Dim Cost As String
Dim Supply As String
Dim EAO As String
Item = InputBox("What is the item number?", "Item Number")
Description = WorksheetFunction.VLookup(Item, Range("A:AJ"), 5, False)
FlyerFEM = WorksheetFunction.VLookup(Item, Range("A:AJ"), 2, False)
MargMak = WorksheetFunction.VLookup(Item, Range("A:AJ"), 3, False)
ISR = WorksheetFunction.VLookup(Item, Range("A:AJ"), 4, False)
ATC = WorksheetFunction.VLookup(Item, Range("A:AJ"), 8, False)
Cost = WorksheetFunction.VLookup(Item, Range("A:AJ"), 19, False)
Supply = WorksheetFunction.VLookup(Item, Range("A:AJ"), 36, False)
EAO = WorksheetFunction.VLookup(Item, Range("A:AJ"), 18, False)
MsgBox ("Description: " & Description & vbCrLf & _
"Flyer/FEM: " & FlyerFEM & _
"Margin Maker: " & MargMak & vbCrLf & _
"ISR Week: " & ISR & vbCrLf & _
"Amount To Sell: " & Round(ATC, 0) & vbCrLf & _
"Cost: $" & Cost & vbCrLf & _
"Months of Supply: " & Supply & vbCrLf & _
"E&O Qty: " & Round(EAO, 0)), vbOKOnly, Item
End Sub

i show this error Conversion failed when converting datetime from character string

I want to save the number of fields in the Data Grid View, and then stored in the Database.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sql As String
For i As Integer = 0 To dgvContract.Rows.Count - 1
sql = "INSERT INTO ContractDetails(Customer_ID , Customer_Name , Contract_ID , Contract_Name ,Strat_Date , End_Date , Value , Description) VALUES(" & (dgvContract.Item("dgvCustomerID", i).Value) & ",'" & (dgvContract.Item("dgvCustomerName", i).Value) & "','" & (dgvContract.Item("dgvContractID", i).Value) & "','" & (dgvContract.Item("dgvContractName", i).Value) & "','" & Format(dgvContract.Item("dgvStratingDate", i).Value, "mm/dd/yyyy") & "','" & Format(dgvContract.Item("dgvEndDate", i).Value, " mm/dd / yy") & "','" & (dgvContract.Item("dgvValue", i).Value & "','" & (dgvContract.Item("dgvDescription", i).Value)) & "')"
SqlHelper.ExecuteScalar(My.Settings.conn, CommandType.Text, sql)
Next
End Sub
It shows me this error:
Conversion failed when converting datetime from character string.

Asp.net Sql Insert rows using select case

Im designing a website that allows a user to update a database with information. The problem I am having is that I have created various controls that are hidden when the page loads, but the user can click to unhide to add additional info.
The issue I am coming up against is writing the sql statement to handle identifying what controls are used. I have a viewstate count to let me know which panels are visible or not.
ViewState("count") = CInt(ViewState("count")) + 1
Label1.Text = ViewState("count").ToString()
And then the code behind to write the SQL query I have looks like this
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim myConn As SqlConnection
Dim cmd As SqlCommand
Dim sqlstring As String
Dim dd As String
Dim mm As String
Dim yy As String
dd = Left(TextBox1.Text, 2)
mm = Mid(TextBox1.Text, 4, 2)
yy = Right(TextBox1.Text, 4)
myConn = New SqlConnection("xxx")
myConn.Open()
Select Case Label1.Text
Case Is = "0"
sqlstring = "INSERT INTO Transactions (Date, Account, Payee, Chq_Num, Reference, GST_Rate, Amount, Document_Number, Bank_Account) VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
Case Is = "1"
sqlstring = "INSERT INTO Transactions (Date, Account, Payee, Chq_Num, Reference, GST_Rate, Amount, Document_Number, Bank_Account) VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "') VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList4.SelectedValue & "','" & TextBox7.Text & "','" & TextBox9.Text & "','" & TextBox8.Text & "'," & DropDownList5.SelectedValue & "," & TextBox10.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
End Select
cmd = New SqlCommand(sqlstring, myConn)
cmd.ExecuteNonQuery()
myConn.Close()
Response.Redirect(Request.RawUrl, True)
End Sub
My problem lies in the select case as when case is 1 it is only inserting the first values and not the second set. I have up to 6 "cases" to choose from, each time adding an additional set of values. Sql server is 2008
Any help is appreciated
I may be wrong but in the second statement the clause VALUES appears two two times.
That might be the source of your problem.
as above however you are going to end up with a horribly long string by the 6th case might want to re-engineer it to check >= each value if the previous strings are immutable
From SQL Server 2008 onwards, you can use the multi-values insert clause that looks like this
INSERT [INTO] tbl [(...columns...)]
VALUES (....),(....),(....);
You have constructed your query like this
INSERT [INTO] tbl [(...columns...)]
VALUES (....)
VALUES (....)
VALUES (....)
I would also personally have constructed the string concatenation not using a SELECT-CASE block:
If Label1.Text >= "0" Then
sqlstring = "('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
End If
If Label1.Text >= "1" Then
sqlstring = sqlstring + "," + "('" & yy & "-" & mm & "-" & dd & "','" & DropDownList4.SelectedValue & "','" & TextBox7.Text & "','" & TextBox9.Text & "','" & TextBox8.Text & "'," & DropDownList5.SelectedValue & "," & TextBox10.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
End If
' etc
If for whatever arcane reason the multi-values statement doesn't work, you can always rely on the tried and true multi-statement batch, i.e.
If Label1.Text >= "0" Then
sqlstring = "INSERT ... VALUES (...);"
End If
If Label1.Text >= "1" Then
sqlstring = sqlstring + "INSERT ... VALUES (...);"
End If
If Label1.Text >= "2" Then
sqlstring = sqlstring + "INSERT ... VALUES (...);"
End If
' etc
Note the semicolon statement terminators within the batch.
The issue was not the select case but the viewstate not passing to the sql string correctly, I know possibly not the best solution but passed the label.text to a hidden textbox, which the sql string was able to read properly. Richard's suggestion to use a concatenated IF statement instead of Select Case was a great idea!
Thanks for all the help

GridView isn't paging or sorting when inside a user control

My Gridview won't page or sort. By this I mean that the data in the GridView doesn't change when I try to sort by a column or when I try to page through the GridView.
I have it inside an UpdatePanel on a user control (.ascx). The code below works fine when I try it outside of the user control.
Here's my code:
<asp:GridView runat="server" ID="grdStats" AutoGenerateColumns="false" Width="100%" AllowSorting="true"
AllowPaging="true" PageSize="20" PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="TopAndBottom">
<Columns>
<asp:BoundField DataField="campaignname" HeaderText="Campaign Name" SortExpression="campaignname" />
<asp:BoundField DataField="site_name" HeaderText="Outlet" SortExpression="site_name" />
<asp:BoundField DataField="filename" HeaderText="Media" SortExpression="filename" />
<asp:BoundField DataField="playinterval" HeaderText="Play Interval" SortExpression="playinterval" />
</Columns>
</asp:GridView>
Here's the backend:
Protected Sub grdStats_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdStats.PageIndexChanging
grdStats.PageIndex = e.NewPageIndex
GetCampaignData()
End Sub
Protected Sub grdStats_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdStats.Sorting
If e.SortExpression = ViewState("sortExpr") Then
ViewState("sortExpr") = e.SortExpression & " desc"
Else
ViewState("sortExpr") = e.SortExpression
End If
GetCampaignData()
End Sub
' ######## POPULATE DATA #########
Private Sub GetCampaignData(Optional ByVal CompanyID As Integer = 0)
Dim sql As String = Nothing
' Check user access level.
If Session("userlevel") = 1 Then
' Current user is admin.
' If a company has been filtered, get it's data, else, get all campaign data.
If CompanyID <> 0 Then
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1" & vbCrLf & _
"AND a.companyid = " & CompanyID
Else
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1"
End If
Else
' User is not an admin.
sql = "SELECT" & vbCrLf & _
" a.campaignid," & vbCrLf & _
" a.companyid," & vbCrLf & _
" a.campaignname," & vbCrLf & _
" a.filename," & vbCrLf & _
" startdate," & vbCrLf & _
" enddate," & vbCrLf & _
" a.playinterval," & vbCrLf & _
" a.ActiveStatus," & vbCrLf & _
" b.*," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS c" & vbCrLf & _
" WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _
" AND c.campaignid = a.campaignid" & vbCrLf & _
" ) AS today," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS d" & vbCrLf & _
" WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _
" AND d.campaignid = a.campaignid" & vbCrLf & _
" ) AS past_week," & vbCrLf & _
" (SELECT COUNT(*)" & vbCrLf & _
" FROM campaignstats AS e" & vbCrLf & _
" WHERE e.campaignid = a.campaignid" & vbCrLf & _
" ) AS playcount" & vbCrLf & _
"FROM campaigns AS a" & vbCrLf & _
"INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _
"WHERE a.ActiveStatus = 1" & vbCrLf & _
"AND a.companyid = " & CInt(Session("companyid"))
End If
grdCampaigns.DataSource = get_data(sql)
grdCampaigns.DataBind()
For i As Integer = 0 To grdCampaigns.Rows.Count - 1
grdCampaigns.Rows(i).Cells(2).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(2).Text, 10)
grdCampaigns.Rows(i).Cells(3).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(3).Text, 10)
Next
End Sub
Public Function get_data(ByVal SQLStatement As String) As DataView
'Populates the datatable
Dim dt As Data.DataTable
dt = db.GetDataTable(SQLStatement)
Dim dv As System.Data.DataView = New System.Data.DataView(dt)
If Not ViewState("sortExpr") Is Nothing Then
dv.Sort = ViewState("sortExpr")
Else
dv = dt.DefaultView
End If
Session("dv5") = dv
Return dv
End Function
Any help would be greatly appreciated.
My apologies the code I pasted in the question, although correct for it's purpose, was not the right code relevant to this question. That said, I'd like to thank you Icarus for taking the time to look over my code.
The paging/sorting works. What I did to fix it was to replace the call to GetCampaignData() within the PageIndexChanging and Sorting event handlers to the SQL code and data binding methods required to give me the results I needed.

Insert into Access DB (loop)

I have this code and its coming up with an INSERT INTO statement error...
Its probably something but I have been at it for a while... please help.
'Add items to db'
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.CommandText = "INSERT INTO order(order_date, coupon_id, customer_id, quantity) values('" & System.DateTime.Now & "','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) & "','" & Int32.Parse(objDR("quantity")) &"')"
myCommand2.Connection = con2
con2.Open()
myCommand2.ExecuteReader()
con2.Close()
test.Text += "Order ID: " & objDR("ID") & "Order Date: " & System.DateTime.Now & ", Cust ID: " & custID & ", Quantity: " & objDR("quantity") &" "
Next
End Function
I think you are getting an error by not enclosing the Date inside Pound signs. You have to do this in Jet (Access) when using variables not parameters.
VALUES('#" & DateTime.Now.Date & "#',...
I also took the liberty of refactoring this code for you since you are creating a new connection for each record which is bad news. Use a Try Catch Finally block and move all that stuff outside the For Loop (please see below)
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" <-- etc
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.Connection = con2
con2.Open()
Try
For intCounter = 0 To obDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
myCommand2.CommandText = "INSERT INTO order(order_date,coupon_id,customer_id,quantity)" _
& "VALUES ('#" & System.DateTime.Now.Date & "#','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) _
& "','" & Int32.Parse(objDR("quantity")) & "')"
myCommand2.ExecuteReader()
Next
Catch ex As Exception
'handle errors here
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
Remember to mark as answered if this helps.
I've sorted it out by removing the single quotes. Thanks everybody to contributed to this.

Resources