Rubberlegs
Member
- Joined
- Feb 19, 2009
- Messages
- 7
- Programming Experience
- Beginner
Hello,
I wonder if anyone can help please!!! I am trying to retrieve a row count from an SQL Db which logs incoming / outgoing email messages to and from our organization.
The Windows Form has a Number Up / Down control, a Button and a Data Grid. The user selects the amount of previous days worth of messages they wish to calculate from the up / down control, and clicks the button which executes the SQL statement and enters the results into a DataGrid. All works fine if I enter the days count manually into the SQL Statement and then run the project. However if I try and insert the variable from the value of the up / down control (IncomingDays) into the statement to indicate the number of days, I get the error shown in the subject line of this post. I have changed the values for Server / DB / UID and password so they do not reflect the true credentials.
Hope this makes sense!!
Here is my code:
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
' Declare variables
Dim objConnection As SqlConnection = New SqlConnection("server=SERVER; database=DATABASE; uid=uid; pwd=password;")
'Dim objDataSet As DataSet = New DataSet()
Dim MailMeterDataSet1 As DataSet = New DataSet()
' Declare SQLDataAdapter object
Dim objDataAdapter As New SqlDataAdapter()
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
Dim IncomingDays As Integer
' Set IncomingDays input field
IncomingDays = nmUpDn1.Value
'Assign a New SQL Command to the Select Command property
objDataAdapter.SelectCommand = New SqlCommand()
'Set the SelectCommand properties
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "SELECT COUNT(DISTINCT MessageIdentity) FROM MessageRecipients WHERE SenderInternal = 'N' and RecipientInternal = 'Y' and TimeReceived >= (GETDATE() -' & IncomingDays & ')"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
objDataAdapter.Fill(MailMeterDataSet1, "MessageRecipients") <<< !!!THIS IS WHERE THE ERROR OCCURS!!!!!
objConnection.Close()
dataGrid1.DataSource = MailMeterDataSet1
dataGrid1.DataMember = "MessageRecipients"
objDataAdapter = Nothing
objConnection = Nothing
End Sub
End Class
--------------------
Any ideas?!?!
Many thanks!!!!
Mark
I wonder if anyone can help please!!! I am trying to retrieve a row count from an SQL Db which logs incoming / outgoing email messages to and from our organization.
The Windows Form has a Number Up / Down control, a Button and a Data Grid. The user selects the amount of previous days worth of messages they wish to calculate from the up / down control, and clicks the button which executes the SQL statement and enters the results into a DataGrid. All works fine if I enter the days count manually into the SQL Statement and then run the project. However if I try and insert the variable from the value of the up / down control (IncomingDays) into the statement to indicate the number of days, I get the error shown in the subject line of this post. I have changed the values for Server / DB / UID and password so they do not reflect the true credentials.
Hope this makes sense!!
Here is my code:
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
' Declare variables
Dim objConnection As SqlConnection = New SqlConnection("server=SERVER; database=DATABASE; uid=uid; pwd=password;")
'Dim objDataSet As DataSet = New DataSet()
Dim MailMeterDataSet1 As DataSet = New DataSet()
' Declare SQLDataAdapter object
Dim objDataAdapter As New SqlDataAdapter()
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
Dim IncomingDays As Integer
' Set IncomingDays input field
IncomingDays = nmUpDn1.Value
'Assign a New SQL Command to the Select Command property
objDataAdapter.SelectCommand = New SqlCommand()
'Set the SelectCommand properties
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "SELECT COUNT(DISTINCT MessageIdentity) FROM MessageRecipients WHERE SenderInternal = 'N' and RecipientInternal = 'Y' and TimeReceived >= (GETDATE() -' & IncomingDays & ')"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
objDataAdapter.Fill(MailMeterDataSet1, "MessageRecipients") <<< !!!THIS IS WHERE THE ERROR OCCURS!!!!!
objConnection.Close()
dataGrid1.DataSource = MailMeterDataSet1
dataGrid1.DataMember = "MessageRecipients"
objDataAdapter = Nothing
objConnection = Nothing
End Sub
End Class
--------------------
Any ideas?!?!
Many thanks!!!!
Mark