Results 1 to 10 of 10

Thread: SQL Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    SQL Problem

    Hey all

    im having problems filling a ListView1 object using another form each time i click the button to fire the event nothing get's displayed in the listview1 object can any 1 help

    I have one form called form2 ( and form4 (The form where the search criteria is selected using textboxes)

    The database is called Pictures ad the fields are pictureID, pictureTitle, pictureDescription, pictureData

    here is my code for the button on form4 to load data into the listview1 object. can any 1 help


    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
    
    Dim frm2Search As New Form2
                Dim mySqlCommand As SqlCommand
                mySqlCommand = New SqlCommand
                mySqlCommand.Connection = Me.SqlConnection1
                mySqlCommand.CommandType = CommandType.Text
                mySqlCommand.CommandText = "SELECT pictureID, pictureTitle, pictureDescription, pictureData FROM Pictures WHERE pictureTitle LIKE @pictureTitle;"
               mySqlCommand.Parameters.Add(New SqlParameter("@pictureTitle", SqlDbType.Text))
              mySqlCommand.Parameters("@pictureTitle").Value = "%" + Me.TextBoxTitle.Text + "%"
    
    
              frm2Search.ListView1.Items.Clear()
              Try
    
                    mySqlCommand.Connection.Open()
                    Dim myDataReader As SqlDataReader
                    myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.Default)
    
                    Dim pic As picture
    
                    While myDataReader.Read
                        pic = New picture
                        pic.pictureID = myDataReader.GetValue(0)
                        pic.pictureTitle = myDataReader.GetValue(1)
                        pic.pictureDescription = myDataReader.GetValue(2)
                        pic.pictureData = New System.IO.MemoryStream(myDataReader.GetValue(3), True)
    
                        Dim i As ListViewItem = New ListViewItem
                        i.Text = pic.pictureTitle
                        i.Tag = pic
                        frm2Search.ListView1.Items.Add(i)
    
                    End While
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                Finally
                    mySqlCommand.Connection.Close()
                End Try
        End Sub
    Last edited by NOTSOSURE; Mar 4th, 2004 at 05:21 AM.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    TIP: name your controls and forms not just the instances

    Did you notice that in the code you posted that it does not say "ListView2" anywhere your referencing "ListView1"?
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    SORRY I MENT TO SAY LISTVIEW1 can u still spot where i'm going wrong?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    would i place the SQLConnection1 on form where the search criteria is being entered or on the form that wioll be getting the data?

  5. #5
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Ok I understand your problem now...

    you have already loaded an instance of Form2 and it is being displayed when you call this code, right?

    Your problem is that you are not referencing that instance you create a new instance and then not display it.

    To make sure this is your problem modify your code like so

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.  
    3.  
    4. Dim frm2Search As New Form2
    5.             Dim mySqlCommand As SqlCommand
    6.             mySqlCommand = New SqlCommand
    7.             mySqlCommand.Connection = Me.SqlConnection1
    8.             mySqlCommand.CommandType = CommandType.Text
    9.             mySqlCommand.CommandText = "SELECT pictureID, pictureTitle, pictureDescription, pictureData FROM Pictures WHERE pictureTitle LIKE @pictureTitle;"
    10.            mySqlCommand.Parameters.Add(New SqlParameter("@pictureTitle", SqlDbType.Text))
    11.           mySqlCommand.Parameters("@pictureTitle").Value = "%" + Me.TextBoxTitle.Text + "%"
    12.  
    13.  
    14.           frm2Search.ListView1.Items.Clear()
    15.           Try
    16.  
    17.                 mySqlCommand.Connection.Open()
    18.                 Dim myDataReader As SqlDataReader
    19.                 myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.Default)
    20.  
    21.                 Dim pic As picture
    22.  
    23.                 While myDataReader.Read
    24.                     pic = New picture
    25.                     pic.pictureID = myDataReader.GetValue(0)
    26.                     pic.pictureTitle = myDataReader.GetValue(1)
    27.                     pic.pictureDescription = myDataReader.GetValue(2)
    28.                     pic.pictureData = New System.IO.MemoryStream(myDataReader.GetValue(3), True)
    29.  
    30.                     Dim i As ListViewItem = New ListViewItem
    31.                     i.Text = pic.pictureTitle
    32.                     i.Tag = pic
    33.                     frm2Search.ListView1.Items.Add(i)
    34.  
    35.                      
    36.  
    37.                 End While
    38.             Catch ex As Exception
    39.                 MessageBox.Show(ex.Message)
    40.             Finally
    41.                 mySqlCommand.Connection.Close()
    42.  
    43. 'ONLY CHANGE IS BELOW HERE:
    44.                 frm2Search.Show
    45.             End Try
    46.     End Sub

    if this is your problem then show me where you load the instance of form2 that you want to fill the listbox or upload your project...
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    Thanks mate that helps loads

    Thanks again

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    sorry to disturb u again lol

    but i have two more question

    1.

    every time a click the search button no matter how many times i click it a new form4 appears

    so if i click the search button 5 times 5 search forms load.

    how can i stop this i think it's somthing to do with

    Dim frm4open As New Form4

    2.

    each time i perform a search it fills a new form2 with the data and not the form i originaly pressed the search button on

    any ideas how to stop this

    i think it's somthing to do with the

    Dim frm2Search As New Form2

    thanks

  8. #8
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I think at first I misunderstood your setup

    correct me if I am wrong

    form4 is the form you enter all your search information on to and is the form that your code snippit is from

    form2 is the form that displays the results of the query

    The answer to your first problem on another form (not one listed above) you are creating a new instance of form4, to solve this problem you must make sure that after you are done searching you call forms close method eg. [InstanceName].Close. Problem 2 is the same thing as problem 1 you havent been closing the instances of the forms.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  9. #9
    New Member
    Join Date
    Mar 2004
    Location
    London
    Posts
    4
    Hey NotSoSure, just out of curiousity - what method have you used to store images in your database field pictureData?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    Het TheBOI

    I used a fileStream. What that does is reads an image into file stream, and from there into Byte array. So what is does converts the picture to a number of bytes and stores those bytes in to the field pictureData in an SQL database.

    Code:
    'Read Picture into file stream, and from there into Byte array.
            Dim myFileStream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
    
            Dim arrayOfBytes(myFileStream.Length) As Byte
    
            myFileStream.Read(arrayOfBytes, 0, arrayOfBytes.Length)
    
            myFileStream.Close()
    
            'Open SQL Connection
            SqlConnection1.Open()
    
            'Copy entered values into parameters, converting data type where necessary
            SqlCommand1.Parameters("@pictureTitle").Value = PictureTitle
            SqlCommand1.Parameters("@pictureDescription").Value = PictureDescription
            SqlCommand1.Parameters("@pictureOwner").Value = PictureOwner
            SqlCommand1.Parameters("@pictureData").Value = arrayOfBytes
    
    
            'Create variable for returned value from SQL execution
            Dim SQLreturn As Integer
    
            'Protect against problems using a Try...Catch block
            Try
                'Execute the command
                SQLreturn = SqlCommand1.ExecuteNonQuery()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
    
            End Try
    
            'Report back results
            Dim result As String
    
            If SQLreturn <> 1 Then
    
                result = "Picture wasn't inserted successfully"
                MessageBox.Show(result, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                StatusBarPanelStatus.Text = result
    
            Else
    
                result = "Picture Inserted successfully"
                MessageBox.Show(result, "Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information)
                StatusBarPanelStatus.Text = result
    
            End If
    
            'Close SQL Connection
            SqlConnection1.Close()

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