Guest User

Untitled

a guest
Jul 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. Public Function AddtoLibrary(ByVal UserName As String, ByVal Password As String, ByVal BookIDs As String, ByVal Device As String, ByVal Log As String) As String
  2.  
  3. 'Logging
  4. If Log <> "Internal Silent" Then
  5. Dim st As StackTrace = New StackTrace()
  6. Dim sf As StackFrame = st.GetFrame(0)
  7. Dim currentMethodName As MethodBase = sf.GetMethod()
  8. Logging(Log, Device, UserName, currentMethodName.Name)
  9. End If
  10.  
  11. 'Database Connection Variables
  12. Dim ConnectionString As String = ConfigurationManager.ConnectionStrings("CnnStr").ConnectionString
  13. Dim Cnn As New SqlClient.SqlConnection
  14. Dim Comm As New SqlClient.SqlCommand
  15. Dim DA As SqlClient.SqlDataAdapter
  16. Dim DS As DataSet
  17. Dim Trans As Data.SqlClient.SqlTransaction
  18.  
  19. If Trim(UserName) = "" Or Trim(Password) = "" Or Trim(BookIDs) = "" Or Trim(Device) = "" Or Trim(Log) = "" Then
  20. Return Nothing
  21. End If
  22.  
  23. Try
  24. If Cnn.State = ConnectionState.Open Then Cnn.Close()
  25. Cnn.ConnectionString = ConnectionString
  26. Cnn.Open()
  27. DS = New DataSet()
  28. DA = New SqlClient.SqlDataAdapter("SELECT ID From Users WHERE UserName = '" & Replace(UserName, "'", "") & "' AND Password = '" & Password & "'", Cnn)
  29. DA.Fill(DS, "Table")
  30. If DS.Tables("Table").Rows.Count <> 0 Then
  31.  
  32. Dim UserID As Integer = DS.Tables("Table").Rows(0).Item("ID")
  33. If Device = "-1" Then
  34. Return "DEVICE"
  35. End If
  36.  
  37. Trans = Cnn.BeginTransaction(IsolationLevel.ReadCommitted)
  38. For i = 0 To BookIDs.Split(";").GetUpperBound(0) - 1
  39. If Mid(BookIDs.Split(";").GetValue(i).ToString, 1, 2) <> "SB" Then
  40. DS = New DataSet()
  41. DA = New SqlClient.SqlDataAdapter("SELECT Price,Discount From Books WHERE ID = " & BookIDs.Split(";").GetValue(i).ToString, Cnn)
  42. DA.Fill(DS, "Table") // <<<<<<<<<<< HERE I GOT THE EXCEPTION
  43. If DS.Tables("Table").Rows.Count <> 0 Then
  44. If DS.Tables("Table").Rows(0).Item("Price") = 0 Or DS.Tables("Table").Rows(0).Item("Discount") = 100 Then
  45. Comm = New SqlClient.SqlCommand("INSERT INTO Invoices (ID,BookID,UserID,Price,OurShare,PublisherShare,Discount,DiscountType,PaymentDate,PaymentTime,SaleOrderID,SaleReferenceID) VALUES (" & Gen_InvoiceID() & "," & BookIDs.Split(";").GetValue(i).ToString & "," & UserID & ",0,0,0,0,''," & Get_Shamsi() & ",'" & Get_Time() & "','" & 0 & "','" & 0 & "')", Cnn, Trans)
  46. Comm.ExecuteNonQuery()
  47. End If
  48. End If
  49.  
  50. Dim BookID As Integer = BookIDs.Split(";").GetValue(i).ToString
  51. Dim TotalDownload As Integer = 0
  52. DS = New DataSet()
  53. DA = New SqlClient.SqlDataAdapter("SELECT DownloadDeviceNumbers FROM Books WHERE ID = " & BookID, Cnn)
  54. DA.Fill(DS, "Table")
  55. If DS.Tables("Table").Rows.Count <> 0 Then
  56. TotalDownload = DS.Tables("Table").Rows(0).Item("DownloadDeviceNumbers")
  57. Else
  58. Return Nothing
  59. End If
  60. Dim DS1 As New DataSet()
  61. Dim DA1 As New SqlClient.SqlDataAdapter("SELECT DISTINCT([DeviceID]) FROM [ketabchinDB].[dbo].[UserActivations] WHERE BookID = " & BookID & " AND UserID = " & UserID & " AND DeviceID = '" & Device & "'", Cnn)
  62. DA1.Fill(DS1, "Table")
  63. If DS1.Tables("Table").Rows.Count = 0 Then
  64. Comm = New SqlClient.SqlCommand("INSERT INTO UserActivations (UserID,BookID,DeviceID,Date,Time) VALUES (" & UserID & "," & BookID & ",'" & Device & "'," & Get_Shamsi() & ",'" & Get_Time() & "')", Cnn)
  65. Comm.ExecuteNonQuery()
  66. End If
  67.  
  68. Else
  69. DS = New DataSet()
  70. DA = New SqlClient.SqlDataAdapter("SELECT SampleBookPath From Books WHERE ID = " & BookIDs.Split(";").GetValue(i).ToString.Replace("SB", ""), Cnn)
  71. DA.Fill(DS, "Table")
  72. If DS.Tables("Table").Rows.Count <> 0 Then
  73. If Trim(DS.Tables("Table").Rows(0).Item("SampleBookPath")) <> "" Then
  74. Comm = New SqlClient.SqlCommand("DELETE FROM SampleInvoices WHERE BookID = " & BookIDs.Split(";").GetValue(i).ToString.Replace("SB", "") & " AND UserID = " & UserID, Cnn, Trans)
  75. Comm.ExecuteNonQuery()
  76. Dim DS1 As New DataSet()
  77. Dim DA1 As New SqlClient.SqlDataAdapter("SELECT BookID From Invoices WHERE UserID = " & UserID & " AND BookID = " & BookIDs.Split(";").GetValue(i).ToString.Replace("SB", ""), Cnn)
  78. DA1.Fill(DS1, "Table")
  79. If DS1.Tables("Table").Rows.Count = 0 Then
  80. Comm = New SqlClient.SqlCommand("INSERT INTO SampleInvoices (ID,BookID,UserID,Date,Time) VALUES (" & Gen_InvoiceID() & "," & BookIDs.Split(";").GetValue(i).ToString.Replace("SB", "") & "," & UserID & "," & Get_Shamsi() & ",'" & Get_Time() & "')", Cnn, Trans)
  81. Comm.ExecuteNonQuery()
  82. End If
  83. End If
  84. End If
  85.  
  86. End If
  87. Next
  88. Trans.Commit()
  89. Return "SUCCESS"
  90.  
  91. End If
  92. Return "WRONG"
  93. Catch ex As Exception
  94. SaveError(ex)
  95. Return ex.Message
  96. Finally
  97. Cnn.Close()
  98. End Try
  99.  
  100. End Function
Add Comment
Please, Sign In to add comment