Many a times when we are getting data from database and showing them on page in asp.net we came across this error "Conversion from type 'DBNull' to type 'String' is not valid Invalid Cast Exception"
The main reason for this error is that when we are getting data from database and we get Null value and we try to cast it into string or any other data type .We can easily resolve this error by just adding small condition before casting into any data type
I have included syntax in vb as well as c#.Here is the trick what we have to do:
Vb.Net
string s=IIf(IsDBNull(db.rd("column")), String.Empty, db.rd("column"))
C#
string s = ( IsDBNull(db.rd("column")) ? String.Empty : db.rd("column") );
Monday, April 8, 2013
Conversion from type 'DBNull' to type 'String' is not valid Invalid Cast Exception
Upload To FTP with FileUpload control
Sometimes we come across a situation when we want get file from user through file upload and upload it to remote server through ftp.In this type of situation we can just create a request stream variable and using that request stream we can write the contents of fileupload to remote server.For this we have to get the content of file upload control in bytes.Here is a sample how i accomplish the same thing.Here spath is the path ftp path to remote server.ftpuser is user name and ftppassword is password to remote server. Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(spath), System.Net.FtpWebRequest) request.Credentials = New System.Net.NetworkCredential(ftpuser, ftppassword) request.Method = System.Net.WebRequestMethods.Ftp.UploadFile Dim ftpStream As Stream = request.GetRequestStream() ftpStream.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length) ftpStream.Close() ftpStream.Dispose() Note:Don't forget to add following lines at the end of the code ftpStream.Close() ftpStream.Dispose()
Thursday, April 4, 2013
Ternary operator in VB.Net
Recently in one of the project I was working with VB.NET language and I was eager to know if there is a ternary operator equivalent there or not. After searching on internet I have found two ways to do it. We have two operators in vb.net to accomplish this task 1) if(condition,true,false) 2) iif(condition,true,false) You can see that both IIF and If operator has three parameter first parameter is the condition which you need to check and then another parameter is true part of you need to put thing which you need as output when condition is ‘true’. We can use any of the two operators and get same output.
Monday, April 1, 2013
Create setup that runas administrator using Visual Studio
Here is a property named "AdminUser" which can help to achieve your goal. Please follow these steps. 1) Open your Setup Project, View->Launch Conditions. 2) Right click on Launch Conditions, and add a new Condition in your Launch Conditions. 3) Right click the Condition, choose Properties Window. 4) Set Condition to AdminUser. 5) Build and install.
Wednesday, March 27, 2013
Property Does not initialise even though we assign value to it in VB.net
I have came around a strange situation in vb.net when i was assigning value to property it was not at all taking the value.When i debug it ,i was getting Nothing.Actually when i was creating property i got the intellisense.This intellisense created structure of the property.
Now the problem comes when we use this property for setting value.The structure we got for property was
Public Property runs As String
Get
End Get
Set(value As String)
End Set
End Property
Now when we assign value to the property it will not even go to the property in debug mode.After investigating i found that there is a small change that we have to make while using intellisence.In Set we have to give ByVal the all wioll be fine.
So the structure of property should be like
Public Property runs As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Monday, March 25, 2013
Cyrillic(Russian ) characters not displaying in asp.net using mysql 3.5.1
When we are using .net with mysql many a times we face a problem that cyrillic(russian)or any other language characters dont display on page or console as expexted .Instead we get ???.The solution to this problem is that we might be using mysql connector driver that does not support cyrillic characters.I was using mysql connector 3.5.1 and faced the same problem.After lot of googling i didn't get any result.So i changed my mysql connector driver from 3.5.1 to 5.1.12 and it worked . The reason for this is MySQL does not support the full UTF8 character set, 4-byte characters are not allowed.
MYsql 3.5.1 connector does not support all UTF_8 Characters.
Also i changed my connection string as
{Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;User=myUsername; Password=myPassword;Option=3;}
Friday, March 22, 2013
Format text returning from query having newline character.
Sometime we come across a situation that user enter the data with line breaks into database but when we display that data in page it shows data as a single line without formatting.To solve this situation before showing data we have to convert newline characters with "<br/>"
s.Replace(Environment.NewLine, "<br/>")
Subscribe to:
Posts
(
Atom
)