Why would Guid.NewGuid() be generating an empty guid? - asp.net

I have a Guid.NewGuid() call that is creating an Empty Guid.
What would cause such a problem and how can I fix it?
Edit: The code:
<WebMethod()> _
Public Function CreateRow(rowValue As String) as String
Dim rowPointer As Guid = System.Guid.NewGuid()
Dim rowPointerValue As String = rowPointer.ToString()
Try
Dim result as Integer = SqlHelper.ExecuteNonQuery(ConnectionString, "Sproc_Name", rowValue, rowPointer)
Return result
Catch ex as Exception
Throw ex
End Try
End Function
Edit: Turns out that rowPointer was originally being passed to the SqlHelper and not rowPointerValue - This of course is passed as empty, as pointed out in the answers. Changing it to rowPointerValue/rowPointer.ToString() fixed the problem.

This is an old problem in VB.NET. It is only the debug visualizer that is broken.
http://www.thesoftwaredevotional.com/2008/12/guid-visualizer-broken-in-vbnet.html

I had the same thing. Debugging of Guid.NeGuid() was showing that it's empty. Calling .ToString() fixed the situation.

I tested this code in VS2008 and the results are not what I expected. It seems that the new guid is not created until the toString method is called. After stepping through the code rowPointerValue does hold a string representation of the guid.
It seems to be using defferred execution?

I've only experiened this problem in VB.NET and only while debugging. Thanks 48494, for that info.

Try this
Dim g As New Guid();
Dim whereDoYouWantToSeeIt As String = g.ToString();

Related

convert stringbuilder to guid

Hi I've some guids in stringbuilder and I want to store these values in guid array.
Can some one give me idea to convert this. I'm working with vb.net
Thanks in advance
Assumption: your guids are really simply strings.
Than that might help:
Dim SB as new Stringbuilder = "GUID"
Dim GuidList as New List(Of Guid)
Dim myGUID as Guid
If GUID.tryparse(SB.tostring, myGuid)=true then
Guidlist.add(myguid)
else
'Handle the error here
end if
Wrap this in a loop over your Guids, I don't know how you stored them.
Remarks: You might need to use parsexact, see the documentation: http://msdn.microsoft.com/en-us/library/system.guid.aspx
I have written out of my head, so the code might not compile...

Using ParseQueryString function to get values from QueryString

I needed to get values out of a string in a QueryString format, that is such as: data1=value1&data2=value2...
Someone suggested I used HttpUtility.ParseQueryString to get the value, I've searched and searched but I can't find any documentation or implementation of this and the Microsoft documentation for it doesn't explain how it works, can someone tell me hwat I'm doing wrong, my code is below;
Public Shared Sub ProcessString(ByVal Vstring As String)
Dim var As NameValueCollection = HttpUtility.ParseQueryString(Vstring)
Dim vname As String = var.QueryString("VNAME")
End Sub
MSDN has an example.
' Parse the query string variables into a NameValueCollection.
Dim qscoll As System.Collections.Specialized.NameValueCollection = HttpUtility.ParseQueryString(Vstring)
Dim vname As String = qscoll("VNAME")
A NameValueCollection represents a collection of associated String keys and String values that can be accessed either with the key or with the index.
I found the problem, I was referencing everything fine but I was doing it in a separate .VB dependency file, once I did it on the actual code behind of the aspx form that solved the problem and it all worked. So now I'm just passing the string from Codebehind as a specialised.NameValue collection in to the function.

Linq to Sql convert to generic list in VB.NET

I am trying to learn some VB.NET for my coo-op that starts next week, and for that reason i took my portfolio web site that is on C# and just started to converting it to VB.NET to get familiar with the syntax.
I am sure my problem is simple however i have a hard time solving it.
I am trying to grab data with the linq query and then return it as list to bind to the Repeater.
I have the following function:
Public Function getProjects() As List(Of portfolio_website)
Using myPortfolio As New PortfolioDataContext
Try
Dim myProjects = (From p In myPortfolio.portfolio_websites _
Order By p.website_id Descending _
Select New With {.name = p.website_name, .id = p.website_id}).Take(5)
Return myProjects
Catch ex As Exception
Return Nothing
End Try
End Using
End Function
However I am getting an error.
If i would do it in C# then myProjects will be var and in return i will have
return myProjects.ToList();
And it would work fine. However if I try to use ToList() in VB.NET i am getting error that it cannot convert it from ling to sql List to the generic list.
How do i do this convertion in VB.NET ? or maybe there is another way ?
I am decent when it comes to C# however somewhat lost with the VB.NET syntax.
I hate it.
Second after I asked my question i realized my mystake.
It is same as in C#, but if i take
Select New With {.name = p.website_name, .id = p.website_id
then i either need to have a custom object, or i need to take everything.
So i changed my linq to sql to the
Dim myProjects = (From p In myPortfolio.portfolio_websites _
Order By p.website_id Descending _
Select p).Take(5)
Return myProjects.ToList()
and now everything is working.

Session variable removal question. as related to a paged data source

I have inherited some very old ASP.Net code. Written initially in 1.0 and then converted to 2.0 There is a page which uses a custom pager control. That control has the following logic in it:
Private _DataSource As PagedDataSource
Public Property DataSource() As PagedDataSource
Get
If Not IsNothing(Session("DataSource")) Then
_DataSource = Session("DataSource")
Return _DataSource
End If
Return Nothing
End Get
Set(ByVal value As PagedDataSource)
_DataSource = value
If Not IsNothing(value) Then
Session("DataSource") = value
Else
Session("DataSource") = Nothing
End If
End Set
End Property
The page which uses that pager has the following logic in it:
Private PagedData As PagedDataSource
Private Function GetData() As DataTable
Dim DT As New DataTable
Dim CategoryID As Integer = -1
If IsNothing(ddlCategories.SelectedValue) OrElse Not Integer.TryParse (ddlCategories.SelectedValue, CategoryID) Then
CategoryID = -1
End If
Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim myAdapter As New SqlDataAdapter
myAdapter = New SqlDataAdapter("SearchResources", myConnection)
myAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
If SearchText <> String.Empty Then
trFiltered.Visible = True
End If
With myAdapter.SelectCommand.Parameters
.AddWithValue("#CategoryID", CategoryID)
.AddWithValue("#SearchText", SearchText)
.AddWithValue("#CompanyID", CompanyID)
End With
If Not Security.IsSiteAdmin Then
myAdapter.SelectCommand.Parameters.AddWithValue("#UserIsAdmin", 0)
myAdapter.SelectCommand.Parameters.AddWithValue("#FTPUserID", Security.GetUserID(Context.User.Identity.Name))
Else
myAdapter.SelectCommand.Parameters.AddWithValue("#UserIsAdmin", 1)
End If
Try
myAdapter.Fill(DT)
Catch ex As Exception
ErrorLog.LogError(ex.Message, ex.StackTrace, HttpContext.Current.User.Identity.Name, HttpContext.Current.Request.Url.ToString)
HttpContext.Current.Response.Redirect("~/error.aspx", False)
Return Nothing
End Try
Return DT
End Function
Protected Sub ReloadData()
CurrentPage = 0
CheckFilters()
BindData()
End Sub
The task at hand was to remove all session variables. What would be the best way to represent this data without the use of session. Originally i was told to put all session items into a cookie, while this worked for most of the items it will not work for this due to the cookie size limitation, Im also not to keen on the idea of keeping it in ViewState or even if that is an option. Im very new to VB and dont have much expierence re-writting legacy code. Session is not an option because this is being moved into a web-farm and Sticky sessions are turned off so it must work without session.
Any suggestions are greatly appreciated. Sorry if im asking a question that has a painfully obvious answer.
thanks in advance,
-James.
Well, somebody may come by and vote me down... but I'd say your best option is ViewState.
If you are limited as you are to not use SessionState and you are running into a cookie size limitation, I can only think of two other options (Cache and ViewState) and I certainly wouldn't recommend Cache for this.
Now you know you can use a out of process session state (either a SQL Server or a StateServer)?
More info can be found here:
http://msdn.microsoft.com/en-us/library/ms178586.aspx

Getting the Request Variables from an ASP.NET page

I wrote the following function that works about 95% of the time, but I need it to work 100% (obviously):
Public Shared Function getPassedVars() As String
Const keyCount As Integer = 54 ' 54 seems to be the number of parameter keys passed by default (for this web_app).
' there are more if there is a form involved (ie. from search page)
Dim oParams As String = ""
Try
With HttpContext.Current
If .Request.Params.AllKeys.Count > keyCount Then
For i As Integer = 0 To (.Request.Params.AllKeys.Count - (keyCount + 1))
oParams &= String.Format("{0}={1}{2}", .Request.Params.Keys.Item(i), .Request.Params(i), IIf(i < .Request.Params.AllKeys.Count - (keyCount + 1), ";", ""))
Next
End If
End With
Return oParams
Catch ex As Exception
Return Nothing
End Try
End Function
It scrubs the Request.Params object for passed variables, which are in the beginning of the array (the remaining ones are ASP parameters). I am pretty sure I've seen a different way to get these parameters, but I haven't been able to figure it out. Any suggestions?
EDIT
So it looks like I can use the Request.URL.Query to achieve this, I will investigate this and post back.
Here is what I came up with:
Public Shared Function getPassedVars() As String
Dim oParams As String = ""
Dim qString As String = ""
Dim oSplit As New List(Of String)
Try
With HttpContext.Current
qString = .Request.Url.Query
If qString.Length > 0 Then 'do we have any passed variables?
If qString.StartsWith("?") Then qString = qString.Remove(0, 1) 'remove leading ? from querystring if it is there
oSplit.AddRange(qString.Split("&"))
For i As Integer = 0 To oSplit.Count - 1
oParams &= String.Format("{0}{1}", oSplit.Item(i), IIf(i < oSplit.Count - 1, ";", ""))
Next
Return oParams
Else
Return Nothing
End If
End With
Catch ex As Exception
Return Nothing
End Try
End Function
So far so good.
Request.QueryString is a NameValueCollection, so the easiest way to get the "parameters" is to do the following:
foreach (String s in Request.QueryString) {
Response.Write(s + " = " + Request.QueryString[s]);
}
Where is your function located? If it's executing in the page's code behind then you definitely do not need to use the HttpContext variable.
It looks like you are trying to get values from the query string.
For example, for this URL:-
http://www.tempuri.org/mypage.aspx?param1=x&param2=y
I assume you want retreive the values of the query string parameters param1 and param2?
If so, just use:-
Dim param1 as String = Request.QueryString("param1")
Otherwise, if these parameters are contained in a form (an HTTP POST request) then use the method which Mitchel Sellers suggests.
If you know the name you can use the following to get it by key value
Dim myParamValue as String = Request.Form("MyKeyName")
Otherwise, you can loop through the form collection, by key etc, to get the values. The key is, do you really need to be parsing all 54 items? Or are you simply looking for a few specific values?
httpcontext.Current.Request.QueryString("KeyName")
Request.Params will contain the query parameters you're after.
There's no need to parse the info from Request.URL since it's already done for you.

Resources