2
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim ds As DataSet = New DataSet()
    Dim param As SqlParameter = New SqlParameter("@user_code", SqlDbType.Char, 4)
    param.Value = "0016"
    Const sqlstr As String = "select sectors.sector_code,sector_name   from user_sectors inner join sectors on user_sectors.sector_code = sectors.sector_code  where user_code = @user_code  and sectors.sector_code not in ('z')"
    Dim da As SqlDataAdapter = New SqlDataAdapter(sqlstr, _con)
    da.SelectCommand.Parameters.Add("@user_code")
    da.Fill(ds)
    ddl.DataValueField = "sector_code"
    ddl.DataTextField = "sector_name"
    ddl.DataSource = ds.Tables(0)
    ddl.DataBind()    
End Sub
2
  • This is not phrased as a question, just a statement, we have to guess what you want. Commented Oct 4, 2012 at 9:17
  • see my answer to do whole parameter instantiation in one line. Commented Oct 4, 2012 at 9:26

3 Answers 3

2

I guess line

da.SelectCommand.Parameters.Add("@user_code")

should be

da.SelectCommand.Parameters.Add(param)
Sign up to request clarification or add additional context in comments.

Comments

1

You should pass param in add

da.SelectCommand.Parameters.Add(param)

Comments

1

Thats because its a SqlParameterCollection and not a string collection.

If you call the appropriate Add overload it will however make the instance for you, if given enough information.

Like this perhaps,

Dim newParam = da.SelectCommand.Parameters.Add("@user_code", sqlDbType.Char, 4)
newParam.Value = "0016"

or, replace several lines like this

da.SelectCommand.Parameters.Add( _
    New SqlParameter("@user_code", SqlDbType.Char, 4) With { .Value = "0016" }))

or, if you want to use many lines, just add

da.SelectCommand.Parameters.Add(param)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.