Monday, February 13, 2012

Argument not specified for parameter error

I am trying to extract a value out of a cookie and then use that as a parameter to a SQL Select...Where function but I am getting the Argument not specified for parameter error. I assume the value from the cookie is not in the variable that I created but I could be wrong.

What is the proper method for populating a variable from a cookie and using that value as part of a SQL Select...Where function?

After I get a value out of the Select...Where function, I need to use the dataset results in a DropDownList as well as perform the DataBind().

here's some of the code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
if Not Page.IsPostBack then
dim Code as String
Code = Server.HtmlEncode(Request.Cookies("UCodeCookie")("Code"))
label1.Text = Code
GetPropertyCodes(Code)
ddlPropertyCode.DataBind()
end if
End Sub

Function GetPropertyCodes(ByVal Code As String) As System.Data.DataSet
Dim connectionString As String = "server='(local)'; trusted_connection=true; database='master'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [Property_Details_db].[Prop_Code] FROM [Property_Details_db] WHERE ([Property_Details_db].[Code] = @.Code)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_code As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_code.ParameterName = "@.Code"
dbParam_code.Value = Code
dbParam_code.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_code)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet
End Function

Thanks for your help.

ChrisWhat is the value of your Code variable when you are setting the parameter value? Can you debug that and determine that?

Alsternately, are you certain everything is spelled correctly? Can you look at the actual SQL being sent using SQL Profiler?|||I know the value in the Cookie exists because I can look at the cookie and see the values that were written to it during the login page. I have tried pulling the value out of the cookie and placing it in the Text property of a label but the label isn't changing from it's default value. That tells me two things: 1. The Request.Cookie code syntax is not correct or 2. The syntax is correct but the Request.Cookie is not finding the cookie. Since the Request.Cookie command doesn't report an error if the cookie is empty or not there, how can I check that the syntax is correct.

Everything is spelled correctly as far as I can tell. How do I look at the actual SQL being sent using SQL Profiler? If you mean using the query tool to check that my Select statement is correct, I have done that as part of the Wizard when creating the Function.

Chris|||Here is your problem:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
if Not Page.IsPostBack then
dim Code as String
Code = Server.HtmlEncode(Request.Cookies("UCodeCookie")("Code"))
label1.Text = Code
GetPropertyCodes(Code)
ddlPropertyCode.DataBind()
end if
End Sub

Code is a variable local to your Page_Load. YOu then use Code in your GetPropertyCodes() method without declaring it. Place Option Explicit On at the top of ALL code files and this sort of error will not occur.

You need to either make Code a variable at the page level, or retrieve a local copy in GetPropertyCodes().|||Where do I place Option Explicit On in my source code?|||At the top of the code file:

Option Explicit On

or at the top of a page with code, make sure Explicit="True", like so:

<%@. Page Explicit="True"%>|||Douglas,

I made the addition of the Explicit reference as suggested but now I am getting a different error. Can you tell me what this really means?

<ERROR>
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 8: if Not Page.IsPostBack then
Line 9: dim Code as String
Line 10: Code = Server.HtmlEncode(Request.Cookies("UCodeCookie")("Code"))
Line 11: GetPropertyCodes(Code)
Line 12:

</ERROR
I think the error above was because I wasn't logged in and the Cookie had not been created but that is just a guess. If I login to my site and create the cookie, I get this error:

<ERROR
Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Char' does not contain a property with the name Prop_Code.

Source Error:

Line 15: ddlPropertyCode.DataValueField = "Prop_Code"
Line 16: ddlPropertyCode.DataMember = "Table"
Line 17: ddlPropertyCode.DataBind()
Line 18: end if
Line 19: End Sub

</ERROR
Thanks for your help.

Chris|||For the first area, obviously you need to protect against the cookie being Nothing.

For the second, Are you certain that the dataset has Prop_Code as a field? Have you checked by examining the DataSet in the debugger?|||Douglas,

I have added an Is Nothing condition so I can redirect to the login page if the Cookie doesn't exist. I know the code to get the Code value out of the Cookie works because I can create a simple page that runs the same commands to populate the UserCode variable and then populate a label.

The Prop_Code column exists in the database table but I still cannot get the Property Code drop down list to populate. I have scoured the Internet and all of my ASP.Net books (including your book) and I still cannot find any references that will help me. My page comes up with the code below but the drop down list is still blank. I have tried populating the properties in the drop down list instead of doing it in the Code but that doesn't work either. I have also tried using a SQLDataSourceControl but then I can't use my function because the dscontrol parameter for the @.Code isn't set.

Do you have any idea or an Internet resource that can help me?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
if Not Page.IsPostBack then
if Request.Cookies("UCodeCookie") is Nothing then
Response.Redirect("login.aspx")
else
dim UserCode as String
UserCode = Server.HtmlEncode(Request.Cookies("UCodeCookie")("Code"))
GetPropertyCodes(UserCode)
ddlPropertyCode.DataSource = "GetPropertyCodes"
ddlPropertyCode.DataTextField = "Prop_Code"
ddlPropertyCode.DataValueField = "Prop_Code"
ddlPropertyCode.DataMember = "Table"
ddlPropertyCode.DataBind()
end if
end if
End Sub

Function GetPropertyCodes(ByVal UserCode As String) As System.Data.DataSet

Dim connectionString As String = "server='(local)'; trusted_connection=true; database='master'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [Property_Details_db].[Prop_Code] FROM [Property_Details_db] WHERE ([Property_Details_db].[Code] = @.Code)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_code As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_code.ParameterName = "@.Code"
dbParam_code.Value = UserCode
dbParam_code.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_code)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet
End Function

Thanks
Chris|||This:

GetPropertyCodes(UserCode)
ddlPropertyCode.DataSource = "GetPropertyCodes"

should be this:


Dim ds as DataSet
ds=GetPropertyCodes(UserCode)
ddlPropertyCode.DataSource = ds.Tables[0]
|||Douglas,

I get this error:

<ERROR
Compiler Error Message: BC30203: Identifier expected.

Source Error:

Line 15: Dim ds as DataSet
Line 16: ds=GetPropertyCodes(UserCode)
Line 17: ddlPropertyCode.DataSource = ds.Tables[0]
Line 18: ddlPropertyCode.DataTextField = "Prop_Code"
Line 19: ddlPropertyCode.DataValueField = "Prop_Code"

</ERROR>|||Doh! Too many languages. This:

ddlPropertyCode.DataSource = ds.Tables[0]

must be this:

ddlPropertyCode.DataSource = ds.Tables(0)

Parens rather than square brackets...|||Douglas,

That worked. I really appreciate your help.

Could you also help me with one other thing? Now that I have the dropdownlist working, I want to retrieve some information out of another SQL Server table after I submit all of the information for this page based on the same code value that I can then place into another cookie. I am not sure how to get the data out of the dataset (read that as function) and place it into a String variable which can in turn be written to the cookie. Do you have any idea how to do this or at least point me to a resource on the web that can help.

Thanks

Chris|||Given a dataset ds that you have filled as previous examples:

Dim c as string
c=ds.Tables(0).Rows(0)("FieldName")

No comments:

Post a Comment