Showing posts with label dim. Show all posts
Showing posts with label dim. Show all posts

Saturday, March 31, 2012

System.FormatException: Input string was not in a correct format.

Strange error mblè mblè :)


Dim SQL = "SELECT confermato, confrndnum FROM utenti WHERE confermato = 'false' AND confrndnum = " & Qs
Dim RsCommand = New OleDbCommand(Sql, Conn)
Dim RsRecordCount = RsCommand.ExecuteScalar()

If RsRecordCount = 0 Then
divErrEx.Visible = True
Else
SQL = "UPDATE utenti SET confermato = 'true' WHERE ConfRndNum = " & QS
RsCommand = New OleDbCommand(Sql, Conn)
RsCommand.ExecuteNonQuery()
Dim ReadCookie = Request.Cookies("tristizia_auth")
ltrUsername.Text = ReadCookie("Username")
divConfOp.Visible = TRUE
End If


the line "If RsRecordCount = 0 Then" gives error
The error is strange becouse if RsRecordCount is 0 the page works, if not it gives the error

Thanks for any answer
Bye
. AtariVerify that the following line is not returning NULL. If it is and you are trying to put it into a string then it won't work of course. The line below is defiantely your problem I think though.


ltrUsername.Text = ReadCookie("Username")

Ok tryed, but the error appears =

I tryed either so:


If RsRecordCount = 0 Then
divErrEx.Visible = True
Else
SQL = "UPDATE utenti SET confermato = 'true' WHERE ConfRndNum = " & QS
RsCommand = New OleDbCommand(Sql, Conn)
RsCommand.ExecuteNonQuery()
Dim ReadCookie = Request.Cookies("tristizia_auth")
If Not ReadCookie Is Nothing Then
ltrUsername.Text = ReadCookie("Username")
Else
ltrUsername.Text = "[Unsknown]"
End If
divConfOp.Visible = TRUE
End If

But however I get error either so :


If RsRecordCount = 0 Then
divErrEx.Visible = True
Else
Dim MyVar = "ewruowehr"
End If

Boh?!?!

Bye
. Atari
One more victim of the Visual Basic type sloppiness (which is a feature).
Try to Dim ReadCookie as HttpCookie, just to be sure you don't get a string instead.
Uhm nothing is changed...


Dim Conn = New OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/mdb-database/tristizia.mdb"))
Conn.Open()
Dim SQL = "SELECT confermato, confrndnum FROM utenti WHERE confermato = 'false' AND confrndnum = " & Qs
Dim RsCommand = New OleDbCommand(Sql, Conn)
Dim RsRecordCount = RsCommand.ExecuteScalar()

<b style="color:red;">If RsRecordCount = 0 Then</b>
divErrEx.Visible = True
Else
SQL = "UPDATE utenti SET confermato = 'true' WHERE ConfRndNum = " & QS
RsCommand = New OleDbCommand(Sql, Conn)
RsCommand.ExecuteNonQuery()
Dim ReadCookie As HttpCookie = Request.Cookies("tristizia_auth")
If Not ReadCookie Is Nothing Then
ltrUsername.Text = ReadCookie("Username")
Else
ltrUsername.Text = "[Sconosciuto]"
End If
divConfOp.Visible = TRUE
End If

But what's the problem? I need to declare AS [type] the variables ? If so what's RsRecordCount ? I tryed As Integer but doesn't work...

Bye
. Atari
It's always better to declare the type of your variables. Not declaring it is a source of bugs.
Why do you use ExecuteScalar here, whereas your query returns two different columns? ExecuteScalar returns the first column of the first row. Here, it would be confermato. Is this column an integer?
In this case, you can Dim RsRecordCount as Integer = CType(RsCommand.ExecuteScalar(), Integer), but I suspect this is not what you're trying to do...
Your query should probably be "SELECT COUNT(*) FROM utenti WHERE confermato = 'false' AND confrndnum = " & Qs

Monday, March 26, 2012

System.net.mail

Simple vb code, right?

Dim objSMTPAs New SmtpClient("mailserver.domain.com", 25)Dim objMsg asNew MailMessage("To@dotnet.itags.org.domain.com","from@dotnet.itags.org.domain.com","test","body testing")objSMTP.Send(objMsg)

When I run the above code, I get an authentication failure. I've tried using a non-default authentication like this:

Dim objMsgAs New MailMessage("from@dotnet.itags.org.domain.com","To@dotnet.itags.org.domain.com","Test","Body of message. Testing 1 2 3")Dim objSMTPAs New SmtpClient("mail.server.com") objSMTP.UseDefaultCredentials =False Dim objCredsAs New NetworkCredential objCreds.UserName ="username" objCreds.Password ="password" objSMTP.Credentials = objCreds objSMTP.Send(objMsg)
But I still get the same error. How do I give the mail server my username and password?

what about putting settings in the configuration file?

 <system.net><mailSettings> <smtp deliveryMethod="network"> <network host="mail.domain.com" userName="username OR DomainEmail" password="Password" defaultCredentials="true"/></smtp></mailSettings> </system.net>


have you tried like this ?

http://www.systemnetmail.com/faq/4.2.aspx

it's only slightly different

objSMTP.Credentials = New NetworkCredential("username", "secret")

Saturday, March 24, 2012

system.net.socket question

I have the following code

Public Sub StartListening()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](128000) {}

' Establish the local endpoint for the socket.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, XXXXX)

' Intializes a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)

' Bind the socket to the local endpoint and listen for incoming
' connections.
Try
listener.Bind(localEndPoint)
listener.Listen(100)

While True
' Set the event to nonsignaled state.
allDone.Reset()

' Start an asynchronous socket to listen for connections.
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
listener)

' Wait until a connection is made before continuing.
allDone.WaitOne()
End While
Catch e As Exception
'Print out error message to log file
' Console.WriteLine(e.ToString()) '************
End Try
End Sub

Private Sub AcceptCallback(ByVal ar As IAsyncResult)
' Signal the main thread to continue.
allDone.Set()

' Get the socket that handles the client request.
Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)

' Create the state object.
Dim state As New StateObject
state.xSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, _
New AsyncCallback(AddressOf readCallback), state)

End Sub

how do I get the IP address from the clients that connect to my sever?nevermind...i figured it out

Trey

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

Tuesday, March 13, 2012

system.text.stringBuilder

how can I add a variable in my system.text.stringbuilder

for example notice the example below

Dim myvar as string
myvar = "something"

Dim mystring as system.text.stringBuilder

mystring.append (" myvar ")

the above code does not work. It puts myvar as the literal text and not the actual value of the variable.

Please help with the syntax.You have to take the quotes out:


mystring.Append(myvar)

Thanks! Another question :) How can i retrieve an item from my datalist. For example I can get the Datakey but how can I get a particular item?

Here is the code I use to obtain the datakeys

Dim dlItem As DataListItem
For Each dlItem In detailsgrid.Items
' reference the TextBox
Dim txtItems As TextBox = CType(dlItem.FindControl("item_received"), TextBox)
' Grab the value
item_recieved = txtItems.Text
' Get the order number from DataKeys
order_num = detailsgrid.DataKeys(dlItem.ItemIndex).ToString()