Showing posts with label querystring. Show all posts
Showing posts with label querystring. Show all posts

Saturday, March 31, 2012

System.InvalidCastException:

I have a page that gets a value from the querystring. I believe people click certain javascript functions on the page that sometimes appends a "#". I have never seen a page error out to this, but I am getting my logfile full of errors like the one below?

System.InvalidCastException: Cast from string "146586#" to type 'Integer' is not valid.

What would be a good way to stop this? Should I somehow trim the very last character if its equal to a "#" .

If so, how can I achieve this?

Thanks
mike123That is weird. The only time I have seen a # appended is when somebody uses the onclick in a A tag

for instance:Click me

In my opinion and to avoid these @.$R # signs is to use this:

Click Me

Saturday, March 24, 2012

System.NullReferenceException

Hi

I try to use this piece of code...


Dim CatID As Integer
CatID = (Request.QueryString("ID"))
CatID = 1
myProductsByCategory.Text = (GetProductsByCategory(CatID))

inside my Private Sub Page_Load but I get a System.NullReferenceException error from this.

What am I missing here?

Regards

MMy guess would be that the Querystring "ID" doesnt exist and you are getting a NULL exception when VB is converting it to an integer.
I hit Post too soon.. you might want to try to comment out the line that is assigning the value from the Querystring, since you are hardcoding it anyway (to 1).. see if thats the issue.
Hi

I comment out the querystring part, but that didn't help...

Regards

M
By the way... the function I try to call looks like this..

Function GetProductsByCategory(ByVal intID As Integer)

Dim connStr As String
connStr = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connStr")

Dim sb As StringBuilder = New StringBuilder
Dim sbOutput As String

Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim MyDataReader As SqlDataReader

MyConnection = New SqlConnection(connStr)
Try
MyConnection.Open()
MyCommand = New SqlCommand("dbo.p_ShowProductsByCategories", MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure
MyCommand.Parameters.Add("@.ID", SqlDbType.Int, 4).Value = intID
MyDataReader = MyCommand.ExecuteReader()

sb.Append("<table width=""123"" border=""0"" cellpadding=""0"" cellspacing=""0"">")

While MyDataReader.Read
sb.Append("<tr>")
sb.Append("<td class=""menuitem"" onmouseover=""this.bgColor='#EEEEEE'"" onmouseout=""this.bgColor='#FDFDFD'"" style=""HEIGHT: 16px""> <a ref=""productdetail.aspx?ID=""" & (MyDataReader("ID")) & """>" & (MyDataReader("Description")) & "</a></td>" & vbCrLf)
sb.Append("</tr>")
End While

sb.Append("</table>")

sbOutput = sb.ToString()

MyDataReader.Close()
MyConnection.Close()

Catch ex As Exception
myErrorCode.Visible = True
myErrorCode.Text = (" ! An error occured <br> while accesing<br> products")
'Response.Write(ex.Message.ToString)
End Try
GetProductsByCategory = sbOutput
End Function

Regards

M
Ok.. is the runtime error not giving you the line number of the error? Are you sure the error is happening in those original 4 lines of code posted?
Try setting a breakpoint on the offending line and stepping through the GetProductsByCategory method to see where the problem lies.

(If you don't have Visual Studio you can use the CLR debugger in the .net framework SDK. You'll find it at

C:\Program Files\Microsoft.NET\FrameworkSDK\GuiDebug\DbgCLR.exe

You'll need to attach it to the aspnet_wp.exe process first. Choose "Debug Processes..." on the "Tools" menu, select aspnet_wp.exe and click "Attach".)
You are right, sorry! The line where the error is ...

myProductsByCategory.Text = (GetProductsByCategory(1))

Regards

M
Ok.. best case, your myProductsByCategory.Text is the problem.. worse case its a problem in the function, which is most likely the problem :)


connStr = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connStr")

Are yousure that you have this entry in your web.config file?
Yes I have, I use that part in the sub page_load so I know thats working.

Regards

M
Time to step through your code and see whats up then :)
Hmmm... I found the problem, I have forgotten to add the runat=server to the label holding the result.

Regards

M

Thursday, March 22, 2012

System.NullReferenceException: Object reference not set to an instance of an object.

Hi all,

I have the following peice of code:

Esssentially, a querystring containing a comma separated list of values is
used to populate the appropriate intSectionID property of a user control.

<% If Not(Request.QueryString("SectionID") Is Nothing) Then %
<% arrSectionID = Split(Request.QueryString("SectionID"), ",") %>
<Company:Links ID="LevelTwo" intSectionID="<%# arrSectionID(0) %>"
Runat="Server"/
<% End If %
However i keep gettting the error listed in my post..

Can someone fill me in on what could be going wrong?

I have a suspicion it may be occuring when the User Controls DataBind method
is called.
The variable is visible to the parent page, but not the user control??

Suggest a way to fix?

Cheers,
AdamAdam,
This is aspnet user group. <%# %> code is ASP. Yes a few things are still
used <% %> this is only available for backwards compatibility not new
development.
Use the page_load or Page_render events for processing the querry string
into your control.

Bad luck

"Adam Knight" wrote:

> Hi all,
> I have the following peice of code:
> Esssentially, a querystring containing a comma separated list of values is
> used to populate the appropriate intSectionID property of a user control.
> <% If Not(Request.QueryString("SectionID") Is Nothing) Then %>
> <% arrSectionID = Split(Request.QueryString("SectionID"), ",") %>
> <Company:Links ID="LevelTwo" intSectionID="<%# arrSectionID(0) %>"
> Runat="Server"/>
> <% End If %>
> However i keep gettting the error listed in my post..
> Can someone fill me in on what could be going wrong?
> I have a suspicion it may be occuring when the User Controls DataBind method
> is called.
> The variable is visible to the parent page, but not the user control??
> Suggest a way to fix?
> Cheers,
> Adam
>