Retrieve a specific column from a row in SQL DB via VB.Net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Retrieve a specific column from a row in SQL DB via VB.Net

  1. #1
    Join Date
    Apr 2005
    Posts
    7

    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

  2. #2
    Join Date
    Nov 2004
    Location
    Oslo
    Posts
    73
    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

  1. help with sparse matrix
    By jrhaley123 in forum Java
    Replies: 8
    Last Post: 09-28-2013, 11:48 PM
  2. Replies: 0
    Last Post: 10-04-2005, 09:13 AM
  3. SQL Server, Variable Column Names
    By JamesGale in forum Database
    Replies: 2
    Last Post: 07-12-2005, 07:58 PM
  4. MSHFlexGrid: get column and row total?
    By Victor in forum VB Classic
    Replies: 1
    Last Post: 04-07-2000, 12:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center