-
Retrieve a specific column from a row in SQL DB via VB.Net
Need some help.
I have a function in VB.net that is supposed to retrieve the mailaddress of the admin for a department. This is stored in SQL2000
I have a table called recipients with the following columns:
Department, Name, E-mail . I have specified Department as Primary key.
The code is suppose to retrieve the row that corresponds to the actual department, and fetch the value from column E-mail.
When I testrun it I get an error saying "MissingPrimaryKeyException. Table doesn't have a primary key".
But a have specified department as key. (Have the key symbol to the left if I chech table design).
Any ideas what could be wrong. My code is below. Not sure if the code is flawless.
Public Function GetMailRecipient(ByVal mventry As MVEntry, ByVal OU As String) As String
Dim mailrecipient As String
' Open a connection to the database.
' Dim strConnection As String = "Data Source=localhost; Initial Catalog=NotificationDB;" _
' & "Integrated Security=True"
Dim ConStr As String = "Server=localhost;Database=NotificationDB;UID=sa;PW D=xxxx"
Dim Conn As SqlConnection = New SqlConnection(ConStr)
Try 'Open the connection.
Conn.Open()
' Set up a data set command object.
Dim strSelect As String = "SELECT * FROM recipients"
Dim Da As New SqlDataAdapter(strSelect, Conn)
' Load a data set.
Dim Ds As New DataSet
Da.Fill(Ds, "recipients")
' Get a reference to the "recipients" DataTable.
Dim dt As DataTable = Ds.Tables.Item("recipients")
' Retrieve the row with the correct mailrecipient for that OU.
Dim MatchRow As DataRow
MatchRow = Ds.Tables("recipients").Rows.Find("OU")
mailrecipient = (MatchRow("mailaddress"))
' For test purpose only
Throw New UnexpectedDataException(mailrecipient)
' Return mailrecipient
' Finally, Close the connection.
Try
Conn.Close()
Catch Err As Exception
Throw New UnexpectedDataException(Err.ToString())
End Try
Catch Err As Exception
Throw New UnexpectedDataException(Err.ToString())
End Try
End Function
Thanks in advance
Patrik
-
If remember correctly the Find() method requires that the table has a primary key. If it doesn't you must use the DataTable.Select() method
oes
Similar Threads
-
By jrhaley123 in forum Java
Replies: 7
Last Post: 11-19-2005, 07:18 PM
-
By mycwcgr in forum ASP.NET
Replies: 0
Last Post: 10-04-2005, 08:13 AM
-
By JamesGale in forum Database
Replies: 2
Last Post: 07-12-2005, 06:58 PM
-
By Victor in forum VB Classic
Replies: 1
Last Post: 04-07-2000, 11:35 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks