Crystal report not showing - vb.net
I created a crystal report and after running it was outputting all the details as required. am surprised now that when I run the same report now the report is showing blank fields with no data below is my code.
Try
Dim rpt As New rptCheckoutNewReceipt 'The report you created.
Dim myConnection As SqlConnection
Dim MyCommand, MyCommand1, MyCommand2, MyCommand3, MyCommand4, MyCommand5 As New SqlCommand()
Dim myDA, myDA1, myDA2, myDA3, myDA4, myDA5 As New SqlDataAdapter()
Dim myDS As New DataSet 'The DataSet you created.
Dim pdfFile As String = Application.StartupPath & "\Report.Pdf"
myConnection = New SqlConnection(cs)
MyCommand.Connection = myConnection
MyCommand1.Connection = myConnection
MyCommand2.Connection = myConnection
MyCommand3.Connection = myConnection
MyCommand4.Connection = myConnection
MyCommand5.Connection = myConnection
MyCommand.CommandText = "SELECT CheckInTime,CheckOutTime, OtherCharge, Room.R_ID, Room.RoomNo, Room.PlanCode, Room.RoomType, Room.MaxNoOfAdults, Room.MaxNoOfKids, Room.Active, CheckIN_Room.Cin_Id, CheckIN_Room.GuestID,CheckIN_Room.RoomID, CheckIN_Room.RoomCharges, CheckIN_Room.DateIN, CheckIN_Room.DateOUT, CheckIN_Room.NoOfDays, CheckIN_Room.NoOfMales, CheckIN_Room.NoOfFemales, CheckIN_Room.NoOfKids, CheckIN_Room.NoOfExtraBed, CheckIN_Room.NoOfExtraPerson, CheckIN_Room.RoomOrderCharges, CheckIN_Room.ExtraPersonCharges, CheckIN_Room.TotalRoomCharges, CheckIN_Room.ExtraBedCharges, CheckIN_Room.OtherCharges, CheckIN_Room.DiscountPer, CheckIN_Room.DiscountAmount, CheckIN_Room.STPer, CheckIN_Room.STAmount, CheckIN_Room.LuxuryTaxPer, CheckIN_Room.LuxuryTaxAmount, CheckIN_Room.SubTotal, CheckIN_Room.GrandTotal, CheckIN_Room.TotalPaid, CheckIN_Room.Balance, CheckIN_Room.Status, CheckIN_Room.Notes, Guest.ID, Guest.GuestID AS Expr2, Guest.GuestName, Guest.Address, Guest.City, Guest.Country, Guest.ContactNo, Guest.Gender, Guest.Occupation, Guest.Religion, Guest.Email, Guest.IDType, Guest.IDNumber, Guest.Pic1, Guest.Pic2, Guest.pic3, Guest.Notes AS Expr3, Guest.Company, Guest.Model, Guest.VehicleNo, Guest.Color FROM Room INNER JOIN CheckIN_Room ON Room.R_ID = CheckIN_Room.RoomID INNER JOIN Guest ON CheckIN_Room.GuestID = Guest.ID where Status='Checked Out' and Cin_ID=" & txtCheckInID.Text & ""
MyCommand1.CommandText = "SELECT * from Hotel"
MyCommand2.CommandText = "SELECT * from Currency"
MyCommand3.CommandText = "SELECT Laundry_BillInfo.Id, Laundry_BillInfo.BillNo, Laundry_BillInfo.BillDate, Laundry_BillInfo.CheckInID, Laundry_BillInfo.GrandTotal, Laundry_BillInfo.PaymentMode, Laundry_BillInfo.TotalPayment,Laundry_BillInfo.PaymentDue, Laundry_BillInfo.LB_Status, LaundryServices.LS_Id, LaundryServices.BillID, LaundryServices.Service, LaundryServices.Rate, LaundryServices.Qty, LaundryServices.TotalAmount,CheckIN_Room.Cin_Id, CheckIN_Room.GuestID, CheckIN_Room.RoomID, CheckIN_Room.RoomCharges, CheckIN_Room.DateIN, CheckIN_Room.DateOUT, CheckIN_Room.NoOfDays, CheckIN_Room.NoOfMales, CheckIN_Room.NoOfFemales, CheckIN_Room.NoOfKids, CheckIN_Room.NoOfExtraBed, CheckIN_Room.NoOfExtraPerson, CheckIN_Room.RoomOrderCharges, CheckIN_Room.ExtraPersonCharges,CheckIN_Room.TotalRoomCharges, CheckIN_Room.ExtraBedCharges, CheckIN_Room.OtherCharges, CheckIN_Room.DiscountPer, CheckIN_Room.DiscountAmount, CheckIN_Room.STPer,CheckIN_Room.STAmount, CheckIN_Room.LuxuryTaxPer, CheckIN_Room.LuxuryTaxAmount, CheckIN_Room.SubTotal, CheckIN_Room.GrandTotal AS Expr1, CheckIN_Room.TotalPaid, CheckIN_Room.Balance,CheckIN_Room.Status, CheckIN_Room.Notes FROM Laundry_BillInfo INNER JOIN LaundryServices ON Laundry_BillInfo.Id = LaundryServices.BillID INNER JOIN CheckIN_Room ON Laundry_BillInfo.CheckInID = CheckIN_Room.Cin_Id where Status='Checked Out' and Cin_ID=" & txtCheckInID.Text & " order by BillNo"
MyCommand4.CommandText = "SELECT Room_OrderInfo.Id, Room_OrderInfo.BillNo, Room_OrderInfo.BillDate, Room_OrderInfo.CheckInId, Room_OrderInfo.GrandTotal, Room_OrderInfo.PaymentMode, Room_OrderInfo.TotalPayment,Room_OrderInfo.PaymentDue, Room_OrderInfo.Operator, Room_OrderInfo.RO_Status, Room_OrderedProduct.OP_Id, Room_OrderedProduct.BillID, Room_OrderedProduct.Dish_Liquor,Room_OrderedProduct.Volumn, Room_OrderedProduct.TakenFrom, Room_OrderedProduct.VATPer, Room_OrderedProduct.VATAmount, Room_OrderedProduct.STPer, Room_OrderedProduct.STAmount, Room_OrderedProduct.DiscountPer, Room_OrderedProduct.DiscountAmount, Room_OrderedProduct.Rate, Room_OrderedProduct.Quantity, Room_OrderedProduct.Amount, Room_OrderedProduct.TotalAmount,CheckIN_Room.Cin_Id, CheckIN_Room.GuestID, CheckIN_Room.RoomID, CheckIN_Room.RoomCharges, CheckIN_Room.DateIN, CheckIN_Room.DateOUT, CheckIN_Room.NoOfDays, CheckIN_Room.NoOfMales,CheckIN_Room.NoOfFemales, CheckIN_Room.NoOfKids, CheckIN_Room.NoOfExtraBed, CheckIN_Room.NoOfExtraPerson, CheckIN_Room.RoomOrderCharges, CheckIN_Room.ExtraPersonCharges, CheckIN_Room.TotalRoomCharges, CheckIN_Room.ExtraBedCharges, CheckIN_Room.OtherCharges, CheckIN_Room.DiscountPer AS Expr1, CheckIN_Room.DiscountAmount AS Expr2, CheckIN_Room.STPer AS Expr3, CheckIN_Room.STAmount AS Expr4, CheckIN_Room.LuxuryTaxPer, CheckIN_Room.LuxuryTaxAmount, CheckIN_Room.SubTotal, CheckIN_Room.GrandTotal AS Expr5, CheckIN_Room.TotalPaid, CheckIN_Room.Balance, CheckIN_Room.Status, CheckIN_Room.Notes FROM Room_OrderInfo INNER JOIN Room_OrderedProduct ON Room_OrderInfo.Id = Room_OrderedProduct.BillID INNER JOIN CheckIN_Room ON Room_OrderInfo.CheckInId = CheckIN_Room.Cin_Id where Status='Checked Out' and Cin_ID=" & txtCheckInID.Text & " order by BillNo"
MyCommand5.CommandText = "SELECT * from Checkin_Payment where CheckInID =" & txtCheckInID.Text & ""
MyCommand.CommandType = CommandType.Text
MyCommand1.CommandType = CommandType.Text
MyCommand2.CommandType = CommandType.Text
MyCommand3.CommandType = CommandType.Text
MyCommand4.CommandType = CommandType.Text
MyCommand5.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand
myDA1.SelectCommand = MyCommand1
myDA2.SelectCommand = MyCommand2
myDA3.SelectCommand = MyCommand3
myDA4.SelectCommand = MyCommand4
myDA5.SelectCommand = MyCommand5
myDA.Fill(myDS, "Guest")
myDA.Fill(myDS, "Room")
myDA.Fill(myDS, "CheckIn_Room")
myDA1.Fill(myDS, "Hotel")
myDA2.Fill(myDS, "Currency")
myDA3.Fill(myDS, "Laundry_BillInfo")
myDA3.Fill(myDS, "LaundryServices")
myDA4.Fill(myDS, "Room_OrderInfo")
myDA4.Fill(myDS, "Room_OrderedProduct")
myDA5.Fill(myDS, "Checkin_Payment")
rpt.SetDataSource(myDS)
rpt.Subreports(0).SetDataSource(myDS)
rpt.Subreports(1).SetDataSource(myDS)
rpt.SetParameterValue("v1", txtBillNo.Text)
rpt.SetParameterValue("v2", dtpBillDate.Value)
frmReport.CrystalReportViewer1.ReportSource = rpt
frmReport.ShowDialog()
Related
How to speed up pie charts creation in VB Web?
I have a web page that can create multiple pie charts based on an SQL query that executes when the page is loading. However I have noticed that the page loading takes too long (around 30 seconds). I checked the SQL formula and can confirm that it is working fine (results given almost instantly). There are 40 different pie charts the page needs to create, and the page also have the option to generate a single pie chart and that works fast enough. So I can deduce that it's the pie chart generation that is slow when it needs to create 40 of them. Is there a way to help speed up the pie chart creation process? Here is my code for reference. Sub draw_chart1() 'All Machines Dim check1, check2, fi, cnt Dim sql Dim myConnectionString As String = "Provider=SQLOLEDB;" & SQLDB_pp.ConnectionString fi = 0 cnt = 0 PlaceHolder1.Dispose() PlaceHolder2.Dispose() 'Get all model Dim query1 As String = String.Format("SELECT DISTINCT A.MacID FROM dbo.tblMachine A LEFT JOIN dbo.tblDataHdr b on a.MacID = b.MacID where DayID between '" & TextBox1.Text & "' AND '" & TextBox3.Text & "' ORDER BY A.MACID") Dim dt1 As DataTable = GetData(query1) For Q As Integer = 0 To dt1.Rows.Count - 1 'For Q As Integer = 0 To 1 check1 = dt1.Rows(Q)(0).ToString() 'This is the slow query Dim query As String = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON' ) b on b.EventName <> '' and MacID in ('" & check1 & "') ) Z LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM dbo.tblEvtDur WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "' GROUP BY MacID ) B ON A.MacID = B.MacID WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "' AND A.MacID in ('" & check1 & "') group by A.MacID, A.EventName) a ON A.EVENTNAME = Z.EVENTNAME and Z.MacID = a.MacID order by Z.MacID, Z.EventName") Dim mychart As Chart = New Chart ' Dim myplace As PlaceHolder = New PlaceHolder Dim ChartArea1 As ChartArea = New ChartArea Dim Legend1 As Legend = New Legend Dim dt As DataTable = GetData(query) Dim x As String() = New String(dt.Rows.Count - 1) {} Dim y As Integer() = New Integer(dt.Rows.Count - 1) {} For i As Integer = 0 To dt.Rows.Count - 1 x(i) = dt.Rows(i)(1).ToString() ' y(i) = dt.Rows(i)(2).ToString() y(i) = Convert.ToInt32(dt.Rows(i)(2)) Next 'Dim myConnection As New OleDbConnection(myConnectionString) 'Dim myCommand As New OleDbCommand(sql, myConnection) 'mychart.Width = Unit.Pixel(Session("sw") - 100) 'mychart.Height = Unit.Pixel((Session("sh") / 2) - 88) mychart.Width = 600 mychart.Height = 400 mychart.ChartAreas.Clear() mychart.ChartAreas.Add("ChartArea1") mychart.Series.Clear() mychart.Series.Add(0) mychart.Series(0).Points.DataBindXY(x, y) mychart.Titles.Clear() mychart.Titles.Add("[" & Q + 1 & "] " & check1.ToString.ToUpper) mychart.Titles(0).Font = New System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Bold) mychart.Titles(0).BackColor = Color.PaleTurquoise mychart.Titles(0).ForeColor = Color.Black mychart.Series(0).ChartType = SeriesChartType.Pie ' mychart.Series(0).Points.DataBindXY(x, y) mychart.Series(0).LegendText = "#VALX" mychart.Series(0)("BarLabelStyle") = "Center" mychart.Series(0)("pointWidth") = "1" mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid mychart.Series(0).BorderWidth = 2 mychart.Series(0).Label = "#PERCENT" mychart.Series(0).ShadowColor = Color.Gray mychart.Series(0).ShadowOffset = 10 mychart.Series(0).LabelBackColor = Drawing.Color.Cornsilk mychart.Series(0).Font = New Font("Tahoma", 9, FontStyle.Bold) 'Chart1.Series(0).LabelToolTip = "#LABEL Percent: #PERCENT" mychart.Series(0).LegendToolTip = "#VALX - #PERCENT" mychart.Series(0).ToolTip = "#VALX - #PERCENT" mychart.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True mychart.Series(0).CustomProperties = "DrawingStyle=LightToDark" 'new Chart1.Series(0).CustomProperties = "PieLabelStyle=Outside" mychart.ChartAreas("ChartArea1").BorderDashStyle = BorderStyle.Solid mychart.Palette = ChartColorPalette.None mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid mychart.Series(0).BorderWidth = 2 mychart.Series(0).BorderColor = Color.Black mychart.PaletteCustomColors = {Drawing.Color.Black, Drawing.Color.LightGray, Drawing.Color.Blue, Drawing.Color.Yellow, Drawing.Color.Red, Drawing.Color.Orange, Drawing.Color.Green} mychart.Legends.Clear() mychart.Legends.Add(0) 'Chart1.Legends(0).Enabled = True ''Chart1.Legends(0).BackColor = Drawing.Color.LightGreenplace mychart.Legends(0).Font = New Font("Tahoma", 10, FontStyle.Bold) mychart.Legends(0).Docking = System.Web.UI.DataVisualization.Charting.Docking.Bottom 'Chart1.Legends(0).Alignment = Drawing.StringAlignment.Center 'Chart1.Legends(0).BackColor = System.Drawing.Color.Transparent mychart.DataBind() 'myplace.Visible = True If (Q + 1) Mod 2 <> 0 Then PlaceHolder1.Controls.Add(mychart) ' Dim spacer As LiteralControl = New LiteralControl("<p />") ' PlaceHolder1.Controls.Add(spacer) End If 'Exit For If (Q + 1) Mod 2 = 0 Then PlaceHolder2.Controls.Add(mychart) 'Dim spacer As LiteralControl = New LiteralControl("<p />") ' PlaceHolder2.Controls.Add(spacer) End If Next End Sub
Drop down list display 0
strsql = "Select Sup_Name + '-----' + Sup_Code As Supdisplay , Sup_Code From kjm_supplier_profile Order By Sup_Name " cmd = New MySqlCommand(strsql, Conn) rdr = cmd.ExecuteReader strddl.DataSource = rdr strddl.DataTextField = "Supdisplay" strddl.DataValueField = "Sup_Code" strddl.DataBind() strddl.Items.Insert(0, New ListItem("Please Select Supplier", "")) rdr.Close() This is to bind the data to the drop-down list with two data. But the drop-down list display 0
strsql = "Select Sup_Name + '-----' + Sup_Code As Supdisplay , Sup_Code From kjm_supplier_profile Order By Sup_Name " cmd = New MySqlCommand(strsql, Conn) rdr = cmd.ExecuteReader strddl.DataSource = rdr strddl.DataTextField = "Sup_Name " strddl.DataValueField = "Supdisplay " strddl.DataBind() strddl.Items.Insert(0, New ListItem("Please Select Supplier", "")) rdr.Close()
use select case condition with SQL query statement in vb.net
I try to use select case statement with SQL statement I have 4 columns when the lbl1.text equal 1 then x = "column1" and put this value in the SQL statement when I try this code Dim x As String Select Case lbl1.Text Case 1 x = "column1" Case 2 x = " column2" Case 3 x = " column3" Case 4 x = " column4" DGV1.Columns.Clear() Dim dt As New DataTable dt.Clear() DGV1.Visible = True Dim comm2 As New SqlCommand comm2.CommandText = Nothing DGV1.Refresh() comm2.CommandText = "select first_name,last_name,age from students where " + x + "=" + "yes" + "" comm2.Connection = sqlconn sqlconn.Open() dr2 = comm2.ExecuteReader dt.Load(dr2) DGV1.AutoGenerateColumns = True DGV1.DataSource = dt DGV1.Refresh() sqlconn.Close() DGV1.Visible = True comm2.Dispose() End Select and when lbl1.text = 1 or 2 or 3 it doesn't run the SQL statement but when the lbl1.text = 4 it's run SQL statement can any one help me in this code
try this code, i just don't know if the break is the right syntax, but i guarantee you that this is the right code ` Dim x As String Select Case lbl1.Text Case 1 x = "column1" Case 2 x = " column2" Case 3 x = " column3" Case 4 x = " column4" End Select DGV1.Columns.Clear() Dim dt As New DataTable dt.Clear() DGV1.Visible = True Dim comm2 As New SqlCommand comm2.CommandText = Nothing DGV1.Refresh() comm2.CommandText = "select first_name,last_name,age from students where " + x + "=" + "yes" + "" comm2.Connection = sqlconn sqlconn.Open() dr2 = comm2.ExecuteReader dt.Load(dr2) DGV1.AutoGenerateColumns = True DGV1.DataSource = dt DGV1.Refresh() sqlconn.Close() DGV1.Visible = True comm2.Dispose()`
vb.net implict inner join syntax error (missing operator) in query expression
This is the code where I am getting exception message. However this code worked perfect in sql server 2005 but generating error in access. This code working fine in sql server project but in access its generating exception as I mentioned.. Public Function CalculateFeeReciept(ByVal monthid As Integer) As DataTable Dim cmd1 As New OleDbCommand("Select * from mstFeeHead", sqlcon) Dim dtmstFeeHead As New DataTable 'dtmstFeeHead contains all the fee heads id's Dim adp1 As New OleDbDataAdapter(cmd1) adp1.Fill(dtmstFeeHead) cmd1.Dispose() Dim selectedmonth As Integer Dim feeheadid As Integer Dim arr(25) As Integer 'arr contains Fee head id's that should be paid in the selected month If monthid = 13 Then monthid = 1 ElseIf monthid = 14 Then monthid = 2 ElseIf monthid = 15 Then monthid = 3 End If selectedmonth = monthid + 3 Dim m As Integer = 0 For j As Integer = 0 To dtmstFeeHead.Rows.Count - 1 feeheadid = Convert.ToInt32(dtmstFeeHead.Rows(j)(0)) If dtmstFeeHead.Rows(j)(selectedmonth) Then arr(m) = feeheadid m = m + 1 End If Next Dim cmd2 As New OleDbCommand("SELECT txnStudentFeeHead.FeeHeadID, mstFeeHead.FeeHeadName, mstFeePlan.Amount " & _ "FROM " & _ "txnStudentFeeHead " & _ "INNER JOIN " & _ "mstFeeHead " & _ "ON " & _ "txnStudentFeeHead.FeeHeadID = mstFeeHead.FeeHeadID " & _ "INNER JOIN " & _ "mstFeePlan " & _ "ON " & _ "mstFeeHead.FeeHeadID = mstFeePlan.FeeHeadID " & _ "WHERE " & _ "txnStudentFeeHead.StudentID = #StudentID) " & _ "AND " & _ "(mstFeePlan.SessionID = #SessionID) " & _ "AND " & _ "(mstFeePlan.ClassID = #ClassID) ", sqlcon) cmd2.CommandType = CommandType.Text cmd2.Parameters.Add("#StudentID", OleDbType.Integer).Value = StudentID() cmd2.Parameters.Add("#ClassID", OleDbType.Integer).Value = Convert.ToInt32(cmbClass.SelectedValue) cmd2.Parameters.Add("#SessionID", OleDbType.Integer).Value = Convert.ToInt32(cmbSession.SelectedValue) Dim dt As New DataTable 'dt contains all the fee head id's that are alloted to the students Dim adp As New OleDbDataAdapter(cmd2) adp.Fill(dt) cmd2.Dispose() Dim dt2 As New DataTable 'dt2 contains all the fee head id's that are alloted to the student and that should be paid in that particular month ' dt2 contains the filtrate of dt and arr dt2 = dt.Clone() For i As Integer = 0 To arr.Length - 1 For j As Integer = 0 To dt.Rows.Count - 1 Dim dtrow As DataRow = dt2.NewRow() If arr(i) = dt.Rows(j)(0) Then dtrow(0) = arr(i) dtrow(1) = dt.Rows(j)(1) dtrow(2) = dt.Rows(j)(2) dt2.Rows.Add(dtrow) End If Next Next cmd2 = New OleDbCommand("Select Sum(TotalFees) as TotalFees, Sum(LateFees) as TotalLateFees, Sum(OldBalance) as TotalOldBalance, Sum(Discount) as TotalDiscount, Sum(Scholarship) as TotalScholarship, Sum(Concession) as TotalConcession, Sum(AmountReceived) as TotalAmountReceived from txnFeePayment where SessionID=#SessionID and StudentID=#studentid and MonthID=#monthid Group by StudentId,MonthID", sqlcon) cmd2.CommandType = CommandType.Text cmd2.Parameters.Add("#studentid", OleDbType.Integer).Value = StudentID() cmd2.Parameters.Add("#SessionID", OleDbType.Integer).Value = cmbSession.SelectedValue cmd2.Parameters.Add("#monthid", OleDbType.Integer).Value = monthid Dim dtStudentReciept As New DataTable 'dt contains all the fee head id's that are alloted to the students adp = New OleDbDataAdapter(cmd2) adp.Fill(dtStudentReciept) cmd2.Dispose() Dim dtrow1 As DataRow = dt2.NewRow() If (dtStudentReciept.Rows.Count > 0) Then dtrow1(0) = 0 dtrow1(1) = "Total Late Fees" dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(1)) dt2.Rows.Add(dtrow1) dtrow1 = dt2.NewRow() dtrow1(0) = 0 dtrow1(1) = "Total Discount" dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(3)) * -1 dt2.Rows.Add(dtrow1) dtrow1 = dt2.NewRow() dtrow1(0) = 0 dtrow1(1) = "Total Scholarship" dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(4)) * -1 dt2.Rows.Add(dtrow1) dtrow1 = dt2.NewRow() dtrow1(0) = 0 dtrow1(1) = "Total Concession" dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(5)) * -1 dt2.Rows.Add(dtrow1) dtrow1 = dt2.NewRow() dtrow1(0) = 0 dtrow1(1) = "Total Amount Received" dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(6)) * -1 dt2.Rows.Add(dtrow1) Dim totalamount As Integer = 0 For k As Integer = 0 To dt2.Rows.Count - 1 totalamount = totalamount + dt2.Rows(k)(2) Next 'dtrow1 = dt2.NewRow() 'dtrow1(0) = totalamount 'dtrow1(1) = "Current Month Fee" 'dtrow1(2) = totalamount 'dt2.Rows.Add(dtrow1) Else Dim totalamount As Integer = 0 For k As Integer = 0 To dt2.Rows.Count - 1 totalamount = totalamount + dt2.Rows(k)(2) Next 'dtrow1 = dt2.NewRow() 'dtrow1(0) = totalamount 'dtrow1(1) = "Current Month Fee" 'dtrow1(2) = totalamount 'dt2.Rows.Add(dtrow1) End If dgvDisplay.DataSource = dt2 For i As Integer = 0 To dgvDisplay.Columns.Count - 1 dgvDisplay.Columns.Item(i).SortMode = DataGridViewColumnSortMode.NotSortable dgvDisplay.Columns(2).Width = 65 dgvDisplay.Columns(1).Width = 132 Next dgvDisplay.Columns.Item(0).Visible = False 'txtTotalFees.Text = dt2.Rows(dt2.Rows.Count - 1)(0) Return dt2 End Function
There are a couple of things wrong with your query. You havent left any spaces once a line in code is finished and whole query may look like they are separate lines but it is a one long string without any spaces. I have added spaces in the following piece of code at the end of each line. ("SELECT [txnStudentFeeHead].[FeeHeadID],[mstFeeHead].[FeeHeadName]," & _ "[mstFeePlan].[Amount] " & _ "FROM " & _ "[txnStudentFeeHead] " & _ "INNER JOIN " & _ "[mstFeeHead] " & _ You have put your variables in sqaure brackets [] , which means SQL Server will treat them as SQL Server Object(table name, Column Name) names and not as Variables. Remove the Square brackets. "WHERE " & _ "([txnStudentFeeHead].[StudentID] = #StudentID) " & _ "AND" & _ "([mstFeePlan].[SessionID] = #SessionID) " & _ "AND" & _ "([mstFeePlan].[ClassID] = #ClassID) ", sqlcon)
Solved it myself by long time of effort and headache Dim cmd2 As New OleDbCommand("SELECT txnStudentFeeHead.FeeHeadID, mstFeeHead.FeeHeadName, mstFeePlan.Amount, txnStudentFeeHead.StudentID, mstFeePlan.ClassID, mstFeePlan.SessionID FROM (txnStudentFeeHead INNER JOIN mstFeeHead ON txnStudentFeeHead.FeeHeadID = mstFeeHead.FeeHeadID) INNER JOIN mstFeePlan ON mstFeeHead.FeeHeadID = mstFeePlan.FeeHeadID WHERE (((txnStudentFeeHead.StudentID)=#StudentID) AND ((mstFeePlan.ClassID)=#ClassID) AND ((mstFeePlan.SessionID)=#SessionID))", sqlcon) cmd2.CommandType = CommandType.Text cmd2.Parameters.Add("#StudentID", OleDbType.Integer).Value = StudentID() cmd2.Parameters.Add("#ClassID", OleDbType.Integer).Value = Convert.ToInt32(cmbClass.SelectedValue) cmd2.Parameters.Add("#SessionID", OleDbType.Integer).Value = Convert.ToInt32(cmbSession.SelectedValue) Dim dt As New DataTable 'dt contains all the fee head id's that are alloted to the students Dim adp As New OleDbDataAdapter(cmd2) adp.Fill(dt) cmd2.Dispose()
Transactions in vb.net locks table and SELECT gets timeout
I have the following problem: Inside one sqltransaction in vb.net I INSERT data into several tables. it is something like this: ... sqltransaction = connection.begintransaction INSERT into table1 ... for i=1 to x ... INSERT into table2 ... SELECT FROM table3 ... INSERT into table3 ... next i sqltransaction.commit ... Private Sub salveazaCerereaDeOferta() If checkCompletion() = 1 Then Dim conn As New SqlConnection(My.Settings.GestiuneService2012_beSQLConnectionString) conn.Open() Dim sqlTrans As SqlTransaction = conn.BeginTransaction() Try Dim cmdInsertCerereOferta As SqlCommand = conn.CreateCommand cmdInsertCerereOferta.Transaction = sqlTrans cmdInsertCerereOferta.CommandText = "INSERT INTO ListaCereriOferte (IDFurnizor,NrCerereOferta,DataCerereOferta,IDModTransCerereOferta,ObservatiiCerereOferta)" + _ " VALUES (#IDFurnizor,#NrCerereOferta,#DataCerereOferta,#IDModTransCerereOferta,#ObservatiiCerereOferta);" + _ " SELECT SCOPE_IDENTITY();" cmdInsertCerereOferta.Parameters.Add("#IDFurnizor", SqlDbType.Int).Value = CInt(Me.t_IDFurnizor.Text) cmdInsertCerereOferta.Parameters.Add("#NrCerereOferta", SqlDbType.Int).Value = CInt(Me.t_NumarCerereOferta.Text) cmdInsertCerereOferta.Parameters.Add("#DataCerereOferta", SqlDbType.Date).Value = CDate(Me.t_DataCerereOferta.Text) cmdInsertCerereOferta.Parameters.Add("#IDModTransCerereOferta", SqlDbType.Int).Value = 1 cmdInsertCerereOferta.Parameters.Add("#ObservatiiCerereOferta", SqlDbType.NVarChar).Value = Me.t_ObservatiiCerereOferta.Text Dim IDCerereOferta As Integer = cmdInsertCerereOferta.ExecuteScalar Dim ListaPieseCerereOferta = Me.CerereOfertaDataSet.ListaPieseCerereOferta.AsEnumerable Dim ListaNecesarePePiesa = Me.CerereOfertaDataSet.NecesarePeOferta.AsEnumerable Dim PieseOferta = From q In ListaPieseCerereOferta _ Select q Dim cantTotalaPiese = Aggregate q In ListaPieseCerereOferta _ Into Sum(q.Cantitate) Dim salvat As Integer = 0 Dim curIDPiesaDeSchimb As Integer For Each piesa In PieseOferta curIDPiesaDeSchimb = piesa.IDPiesaDeSchimb Dim NecesarePePiesa = From p In ListaNecesarePePiesa _ Select p _ Order By p.IDNecesar Descending Dim curIDNecesar As Integer For Each necesar In NecesarePePiesa curIDNecesar = necesar.IDNecesar Dim cmdInsertPiesaCerereOferta As SqlCommand = conn.CreateCommand cmdInsertPiesaCerereOferta.Transaction = sqlTrans cmdInsertPiesaCerereOferta.CommandText = "INSERT INTO CereriOferte (IDCerereOferta,IDPiesaDeSchimb,Cantitate,UM,Observatii)" + _ " VALUES (#IDCerereOferta,#IDPiesaDeSchimb,#Cantitate,#UM,#Observatii);" + _ " SELECT SCOPE_IDENTITY();" cmdInsertPiesaCerereOferta.Parameters.Add("#IDCerereOferta", SqlDbType.Int) cmdInsertPiesaCerereOferta.Parameters.Add("#IDPiesaDeSchimb", SqlDbType.Int) cmdInsertPiesaCerereOferta.Parameters.Add("#Cantitate", SqlDbType.Float) cmdInsertPiesaCerereOferta.Parameters.Add("#UM", SqlDbType.NVarChar) cmdInsertPiesaCerereOferta.Parameters.Add("#Observatii", SqlDbType.NVarChar) Dim cmdInsertNecesarCerereOferta As SqlCommand = conn.CreateCommand cmdInsertNecesarCerereOferta.Transaction = sqlTrans cmdInsertNecesarCerereOferta.CommandText = "INSERT INTO NecesareCereriOferte (IDPiesaNecesar,IDPiesaCerereOferta)" + _ " VALUES (#IDPiesaNecesar,#IDPiesaCerereOferta)" cmdInsertNecesarCerereOferta.Parameters.Add("#IDPiesaNecesar", SqlDbType.Int) cmdInsertNecesarCerereOferta.Parameters.Add("#IDPiesaCerereOferta", SqlDbType.Int) Select Case curIDNecesar Case 0 cmdInsertPiesaCerereOferta.Parameters("#IDCerereOferta").Value = IDCerereOferta cmdInsertPiesaCerereOferta.Parameters("#IDPiesaDeSchimb").Value = curIDPiesaDeSchimb cmdInsertPiesaCerereOferta.Parameters("#Cantitate").Value = 1 cmdInsertPiesaCerereOferta.Parameters("#UM").Value = piesa.UM cmdInsertPiesaCerereOferta.Parameters("#Observatii").Value = "" For i = 1 To necesar.Cantitate cmdInsertPiesaCerereOferta.ExecuteNonQuery() salvat += 1 Me.tsspb_SalvareCerereOferta.Value = CInt(100 * salvat / cantTotalaPiese) Next Case Is > 0 Me.PieseNecesarePeOfertaTableAdapter.Fill(Me.CerereOfertaDataSet.PieseNecesarePeOferta, curIDNecesar, curIDPiesaDeSchimb, CInt(necesar.Cantitate)) Dim ListaPieseNecesarePeOferta = Me.CerereOfertaDataSet.PieseNecesarePeOferta.AsEnumerable Dim PieseNecesareOferta = From q In ListaPieseNecesarePeOferta _ Select q For i = 1 To necesar.Cantitate cmdInsertPiesaCerereOferta.Parameters("#IDCerereOferta").Value = IDCerereOferta cmdInsertPiesaCerereOferta.Parameters("#IDPiesaDeSchimb").Value = curIDPiesaDeSchimb cmdInsertPiesaCerereOferta.Parameters("#Cantitate").Value = 1 cmdInsertPiesaCerereOferta.Parameters("#UM").Value = piesa.UM cmdInsertPiesaCerereOferta.Parameters("#Observatii").Value = "" Dim insertedIDPiesaCerereOferta As Integer = cmdInsertPiesaCerereOferta.ExecuteScalar cmdInsertNecesarCerereOferta.Parameters("#IDPiesaNecesar").Value = PieseNecesareOferta(i - 1).IDPiesaNecesar cmdInsertNecesarCerereOferta.Parameters("#IDPiesaCerereOferta").Value = insertedIDPiesaCerereOferta cmdInsertNecesarCerereOferta.ExecuteNonQuery() salvat += 1 Me.tsspb_SalvareCerereOferta.Value = CInt(100 * salvat / cantTotalaPiese) Next End Select Next Next sqlTrans.Commit() MsgBox("Cererea de oferta a fost salvata.") Catch ex As Exception Try sqlTrans.Rollback() MsgBox("A aparut o eroare." + vbNewLine + ex.ToString + vbNewLine + "RollBack success. Cererea nu a fost salvata.") Catch ex2 As SqlException If Not sqlTrans.Connection Is Nothing Then MsgBox("An exception of type " & ex2.GetType().ToString() & _ " was encountered while attempting to roll back the transaction.") End If End Try Finally If conn.State = ConnectionState.Open Then conn.Close() End Try Else MsgBox("Nu ai completat integral cererea de oferta!") End If End Sub query that locks: SELECT TOP (100) PERCENT dbo.qry_NecesareNerezolvate.IDPiesaNecesar, COUNT(dbo.NecesareCereriOferte.IDPiesaCerereOferta) AS CereriOferte, dbo.qry_NecesareNerezolvate.IDNecesar, dbo.qry_NecesareNerezolvate.IDPiesaDeSchimb, dbo.qry_NecesareNerezolvate.Cantitate FROM dbo.qry_NecesareNerezolvate LEFT OUTER JOIN dbo.NecesareCereriOferte ON dbo.qry_NecesareNerezolvate.IDPiesaNecesar = dbo.NecesareCereriOferte.IDPiesaNecesar GROUP BY dbo.qry_NecesareNerezolvate.IDPiesaNecesar, dbo.qry_NecesareNerezolvate.IDNecesar, dbo.qry_NecesareNerezolvate.IDPiesaDeSchimb, dbo.qry_NecesareNerezolvate.Cantitate qry_NecesareNerezolvate: SELECT TOP (100) PERCENT dbo.Necesare.IDPiesaNecesar, dbo.Necesare.IDNecesar, dbo.Necesare.IDPiesaDeSchimb, dbo.Necesare.Cantitate, dbo.Necesare.UM, dbo.Necesare.Observatii FROM dbo.Necesare LEFT OUTER JOIN dbo.qry_NecesareComandateRezolvate ON dbo.Necesare.IDPiesaNecesar = dbo.qry_NecesareComandateRezolvate.IDPiesaNecesar WHERE (dbo.qry_NecesareComandateRezolvate.IDPiesaNecesar IS NULL) qry_NecesareComandateRezolvate: SELECT dbo.Necesare.IDPiesaNecesar, dbo.NecesareComenzi.IDPiesaComanda, dbo.NecesareRezolvate.IDBonTransfer FROM dbo.Necesare LEFT OUTER JOIN dbo.NecesareComenzi ON dbo.Necesare.IDPiesaNecesar = dbo.NecesareComenzi.IDPiesaNecesar LEFT OUTER JOIN dbo.NecesareRezolvate ON dbo.Necesare.IDPiesaNecesar = dbo.NecesareRezolvate.IDPiesaNecesar WHERE (dbo.NecesareComenzi.IDPiesaComanda IS NOT NULL) OR (dbo.NecesareRezolvate.IDBonTransfer IS NOT NULL) for i=1: insert, select then insert is working then i=2 and: INSERT into table2... is working but SELECT FROM table1 and table3... gets timeout... I don't understand why! PS: If I break the code before second run of SELECT FROM table3 then go to SSMS and run the select query it gets timeout too...
In your code sample below, you have the transaction open till the end of the For Loop. It seems to me that when i =1 , the transaction gets opened and a insertion is made. Now when i=2 , the to be inserted row when i=1 is in uncomitted state and if your select is joining to the table1 the lock level would have escalated to table level and locked everything down. Move the commit to inside the for loop and check how it goes. for i=1 to x ... INSERT into table2 ... SELECT FROM table3 ... INSERT into table3 ... next i sqltransaction.commit