Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset to create mail attachment

Status
Not open for further replies.

russellmunday

Technical User
Jul 24, 2003
87
GB
I am trying to attach files to an email based on a selection that is stored in a sql table but when i try to loop through whats there i get various build errors.
if anyone has done this or can see where i'm gpoing wrong please help


'connect to database and fill the daaset with the kitnumbers stored in the moving table
Dim wapp As System.Web.Mail.MailMessage
Dim ssql As String
ssql = "select kit from moving"
Dim oconn As New SqlClient.SqlConnection(sconnection)
Dim da As New SqlClient.SqlDataAdapter(ssql, oconn)
Dim ds As New DataSet
'connect and fill
oconn.Open()
da.Fill(ds, "kit")
oconn.Close()
' output results to a datagrid to confirm results so far
dg2.DataSource = ds
dg2.DataBind()
' create the data table
Dim dt As DataTable
dt = ds.Tables("kit")
Dim dr As DataRowCollection
dr = dt.Rows

Dim p As Integer
For p = 0 To dt.Rows.Count

Next

Dim sattach As System.Web.Mail.MailAttachment
Dim i As Integer

ReDim sattach(p)

p = 0

For Each dr In dt.Rows

'create the attachment

sattach(p) = New System.Web.Mail.MailAttachment("c:\attachfiles\" & dr.Item(0) & ".jpg")

p = p + 1
Next


wapp = New System.Web.Mail.MailMessage
With wapp
wapp.From = "webserver@mycompany.com"
wapp.To = "test@mycompany.com"
wapp.Subject = "Thankyou for your order details below"
wapp.Body = "Please see attachments"
wapp.Attachments.Add(sattach)
End With
System.Web.Mail.SmtpMail.SmtpServer = "mysmtpserver"
System.Web.Mail.SmtpMail.Send(wapp)
wapp = Nothing
 
I have already explained this to you in thread855-1077826


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
russellmunday said:
your a star
thanks for your help
So why did you star the post and write the above if it didn't help?!!!

The code that I gave in the last post does work as it is just a simple loop to add each attachment and from your above code you haven't implemented it at all (or taken my advice which was "I'm not sure why you do your loop before setting up the email").


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
had to move on to another project an your help was appreciated but when i came back to this project and tried the code it would not work.
 
What do you mean by it "does not work"? Show me the code you have with my example implemented...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I do not have the origional code i tested with so i have recreated it the first problem is the dr.item is underlined and when i hover the mouse over it it says " overload resolution failed because no accessible item accepts this number of arguments.

Dim da As SqlClient.SqlDataAdapter
Dim dt As New DataTable
Dim dr As DataRow

Dim ssql As String = "select kit from moving"
Dim i As Integer
oconn = New SqlClient.SqlConnection(sconnection)
oconn.Open()
da = New SqlClient.SqlDataAdapter(ssql, oconn)
da.Fill(dt)
oconn.Close()

Dim mAttach As System.Web.Mail.MailAttachment
For Each dr In dt.Rows
'add each attachment
mAttach = New System.Web.Mail.MailAttachment("f:\SOFTWARE\gb" & dr.Item & ".plt")
wapp.Attachments.Add(mAttach)
Next
'Dim sattch(i) As New System.Web.Mail.MailAttachment("f:\SOFTWARE\gb" & dr.Item & ".plt")

wapp = New System.Web.Mail.MailMessage
With wapp
wapp.From = "webserver@mycompany.com"
wapp.To = "me@mycompany.com"
wapp.Subject = "Thankyou for your order details below"
wapp.Body = "Please see attachments"
' wapp.Attachments.Add(attachment)
wapp.Attachments.Add(mAttach)
End With
System.Web.Mail.SmtpMail.SmtpServer = "smtp.dsl.pipex.com"
System.Web.Mail.SmtpMail.Send(wapp)
wapp = Nothing
 
The "dr.Item" problem is that it needs to know which index to use. Try using dr.Item(0) instead.

Also, as I described in the previous thread, you need to set up the attachments within the MailMessage code. In the example above, you have created the attachments before you have even set up the MailMessage.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I actually wrote a FAQ on this a few weeks ago. It uses a web service and while it doesn't exactly address your problem you might want to check it out. You might also want to make a web service out of your code as well, that way you can share it with others around your company. Just a thought.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top