SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mulefeathers
Starting Member

12 Posts

Posted - 11/13/2012 :  12:21:57  Show Profile  Reply with Quote
I am tryign to return the values from a table based on the selected value of a combobox. This code is in a SelectedIndexChanged event of a combobox. If anyone could please read this code and tell me where my error is and point me the correct direction.
I declare my variables, create a connection string and open.

Dim strprocess As String
        Dim strpart As String = Nothing
        strprocess = cboprocess.SelectedValue
        Dim connectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\sql_dbf\kanban card.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

        strpart = "SELECT PartNumber FROM tblParts WHERE Process = @Process"

       
        Dim conn1 As New SqlConnection(connectionString)
        conn1.Open()

Then I am using the next block of code (I think) to create a data table to hold the results of the query.


Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(strpart)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@Process", strprocess)
        cmd.Connection = conn1

        Dim someDatatable As New DataTable
        Dim myReader As SqlClient.SqlDataReader = cmd.ExecuteReader

        If myReader.HasRows Then
            someDatatable.Load(myReader)
        End If

But when I try to compile I get an error:
The parameterized query '(@Process nvarchar(4000))SELECT PartNumber FROM tblParts WHERE P' expects the parameter '@Process', which was not supplied.

Thanks in advanced for any help.



chadmat
The Chadinator

USA
1974 Posts

Posted - 11/13/2012 :  12:37:39  Show Profile  Visit chadmat's Homepage  Reply with Quote
Try this instead of AddWithValue:

cmd.Parameters.Add("@Process", SqlDbType.Int)
cmd.Parameters("@Process").Value = strprocess

-Chad
Go to Top of Page

mulefeathers
Starting Member

12 Posts

Posted - 11/13/2012 :  15:20:37  Show Profile  Reply with Quote
Thanks, its seams to be getting there. This line of code:

Dim myReader As SqlClient.SqlDataReader = cmd.ExecuteReader

Is giving error:
Failed to convert parameter value from a String to a Int32.
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 11/13/2012 :  15:30:59  Show Profile  Visit chadmat's Homepage  Reply with Quote
Do Int.Parse on it

-Chad
Go to Top of Page

mulefeathers
Starting Member

12 Posts

Posted - 11/14/2012 :  09:49:07  Show Profile  Reply with Quote
Could you explain the purpuse of converting the string to an interger?
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 11/14/2012 :  10:34:22  Show Profile  Visit chadmat's Homepage  Reply with Quote
I assumed it was an integer since there were no single quotes in the query. Is Process a string?

-Chad
Go to Top of Page

mulefeathers
Starting Member

12 Posts

Posted - 11/14/2012 :  10:44:15  Show Profile  Reply with Quote
Yes it is a string. Maybe I should use this query instead?

strpart = "SELECT PartNumber FROM tblParts WHERE Process = '" & strprocess & "'"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000