Results 1 to 7 of 7

Thread: Urgent HELP needed on Datagrid

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29

    Angry Urgent HELP needed on Datagrid

    Hi,

    I have 2 tables in SQL Server:

    Transactions Table
    TransNo(PK)
    CustNo
    TransDate
    TotalAmt


    TransDetails Table
    TransNo(PK)
    VideoNo(PK)
    VideoTitle
    DueDate


    One customer has one transNo but more than one VideoNo(if he/she rental more videos.)

    I wanted to display the transDetails for a customer in a grid and heres my coding:

    Private Sub Grid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SqlConnection1.Open()
    cm.Connection = SqlConnection1
    SQLstr = "Select VideoNo, VideoTitle, DueDate from TransDetails JOIN Transactions ON Transactions.TransNo = TransDetails.TransNo WHERE CustNo ='" & txtCustomerNo.Text & "'"
    cm.CommandText = SQLstr

    SqlDataAdapter1.SelectCommand = cm

    SqlDataAdapter1.Fill(DataSet11)

    End Sub

    I do have this error when i run it.

    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.

    what wong with the coding??
    Please help.
    viv


  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    step through your code and note if it happens at connection or after. what is your connection string?

  3. #3
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    also, where are u declaring your dataset?

    here's an example of how I do mine:

    VB Code:
    1. Try
    2.  
    3.             'create a command
    4.             'UPDATEQUE is the name of a stored procedure and the connection to it is SQLCONN
    5.             Dim cmd As New SqlClient.SqlCommand("UpdateQue", SQLCONN)
    6.             'set the type of command (a stored procedure in this case)
    7.             cmd.CommandType = CommandType.StoredProcedure
    8.             'any queries will be in there
    9.             'add parameters to send to the stored procedure.  
    10.             'here, the variable IN the stored procedure is called
    11.             '   @quecode and the value is set to what 'quecode' is
    12.             With cmd.Parameters
    13.                 .Add("@quecode", quecode)
    14.             End With
    15.             'create a data adapter  
    16.             Dim adapter As New SqlClient.SqlDataAdapter(cmd)
    17.             'Create a data set
    18.             Dim dataset As New DataSet(cmd.CommandText)
    19.             'fill the adapter
    20.             adapter.Fill(dataset)
    21.             'Set the datasource for the datagrid
    22.             dg.DataSource = dataset.Tables(0)
    23.  
    24.         Catch ex As SqlClient.SqlException
    25.             MessageBox.Show(ex.Message)
    26.  
    27.         End Try

    hope that helps.
    Last edited by Andy; Jul 30th, 2004 at 08:59 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29
    Hi,

    i am using ADO.net.

    i am still not understand your coding. where do i put the SQL query ??

    viv

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    the example above is using ado.net as well. the query is in a stored procedure. I updated the last post in hopes it will explain a little more for you.
    Last edited by Andy; Jul 30th, 2004 at 08:58 AM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29
    i am using ADO.NET and i have a SqlConnection1, SqlDataAdapter1, Dataset11 and SqlDataAdapter2.

    In the SQL query i need to use 2 tables.

    Transactions Table
    TransNo(PK)
    CustNo
    TransDate
    TotalAmt


    TransDetails Table
    TransNo(PK)
    VideoNo(PK)
    VideoTitle
    DueDate

    1 customer only have one transNo but can have more than one VideoNo. Therfore the SQLstr = "Select VideoNo, VideoTitle, DueDate from TransDetails JOIN Transactions ON Transactions.TransNo = TransDetails.TransNo WHERE CustNo ='" & txtCustomerNo.Text & "'"

    i do have a txtCustomerNo.text with the CustNo.
    The SQLstr can get the right records. But i cannot bind it into the grid. Do i need 2 SqlDataAdapter for 2 tables???

    My coding is :

    Dim cm As New SqlClient.SqlCommand
    Dim SQLstr As String
    Dim dv As DataView
    Dim cm1 As CurrencyManager

    Private Sub Grid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'SqlConnection1.Open()
    cm.Connection = SqlConnection1

    SQLstr = "Select VideoNo, VideoTitle, DueDate from TransDetails JOIN Transactions ON Transactions.TransNo = TransDetails.TransNo WHERE CustNo ='" & txtCustomerNo.Text & "'"
    cm.CommandText = SQLstr

    SqlDataAdapter1.SelectCommand = cm

    SqlDataAdapter1.Fill(DataSet11)

    End Sub


    Please help!!

    viv

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29
    hi,

    still cannot solved the problem, this is the error i got!!

    An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll

    Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

    is it because i set pk in TransNo and VideoNo(pk) in the TransDetails table ???

    how to solve this problem??

    viv

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width