Search VB 2010

Friday, December 09, 2011

Solution - There is already an open DataReader associated with this Command which must be closed first.

VB 2010

Problem : There is already an open DataReader associated with this Command which must be closed first.
Cause : Having more than 1 dataReader result set working at the same time .
Solution : In my solution example, i work with SqlServer2000 and VB.Net 2005 
The Example Project can be downloaded at the end of this post :
It contains (SqlServer2000 DataBase - VB.Net Project)
The example shows how to navigate (First Record - Next Record) using ADO.Net 2.0 Online-Mode using SqlClient DataReader ...
Sql Database Name : Market
Sql Database Table : Market.Info
Server Name : evry1falls
(in order to see the example correctly, you'll have to Import the Database 'Market' into your sqlserver2000 Enterprise Manager)
Photo1 : Application Final Design
Photo 1












Photo2 : Table Structure
Photo 2







Photo3 : Codes
Photo 3






Photo4 : Codes
Photo 4







Photo5 : Codes
Photo 5








Now if you run the project and tried to navigate using (First) or (Next) you will get the Exception :
There is already an open DataReader .....
so the solution would be :
Trap the Exception that shows in the (SelectedIndexChanged) Event of the (ComboBox) like this :

and the same for the (Next) Button ..
now you can Navigate and search using combobox safely without exceptions ..

Note :
- Source code will give you exception and you will have to re-write the solution by yourself  as shown above .
- The Folder(Bin/Debug) Includes (Back-Up) use it to restore the (Market Database) in your sqlserver2000
- use the namespace (evry1falls) as new server Registration .

Download Source Code : MediaFire Link

Using MS Access 2003/2007/2010

- In case of using Microsoft Access Database, OleDb.OleDbDataReader with OleDb.OleDbCommand.

The work around or the solution would be :
Always create a new Instance of the OleDbCommand, I.e

Friday, November 25, 2011

Visual Basic 2010 Math Functions

Microsoft Visual Basic 2010

Math Functions
Visual Basic 2010 Math functions
Visual Basic 2010 Math functions
Math and String Functions
In addition to performing simple arithmetic and string operations with the arithmetic and string operators, Visual Basic programs can take advantage of several built-in mathematical functions and string functions to perform useful processing that, otherwise, could require highly complex original code.
Mathematical Functions
Popular mathematical functions are summarized in the following table. Note that certain functions do not require the Math. prefix.
FunctionUse
Math.Abs()Returns the absolute value.
Math.Abs(-10) returns 10.
Math.Ceiling()Returns an integer that is greater than or equal to a number.
Math.Ceiling(5.333) returns 6.
Fix()Returns the integer portion of a number.
Fix(5.3333) returns 5.
Math.Floor()Returns an integer that is less than or equal to a number.
Fix(5.3333) returns 5.
Int()Returns the integer portion of a number.
Int(5.3333) returns 5.
Math.Max()Returns the larger of two numbers.
Math.Max(5,7) returns 7.
Math.Min()Returns the smaller of two numbers.
Math.Min(5,7) returns 5.
Math.Pow()Returns a number raised to a power.
Math.Pow(12,2) returns 144.
Rnd()Returns a random number between 0 and 1. Used in conjunction with Randomizestatement to initialize the random number generator.
Math.Round()Rounds a number to a specified number of decimal places. Rounds up on .5.
Math.Round(1.1234567,5) returns 1.12346.
Math.Sign()Returns the sign of a number. Returns -1 if negative and 1 if positive.
Math.Sign(-5) returns -1.
Math.Sqrt()Returns the square root of a positive number.
Math.Sqrt(144) returns 12.
Random Numbers
The Rnd() function returns a random number between 0 and 1. More likely, the need is to generate a number within a particular range, between a given low and high number. This is accomplished with the following formula.
Math.floor((high - low + 1) * Rnd() + low)
For instance, to generate a random number between 0 and 10 the formula becomes
Math.floor((10 - 0 + 1) * Rnd() + 0)
String Functions
Several built-in string functions perform string manipulations to augment simple concatenation with the "&" operator. These functions are summarized in the following table.
FunctionUse
Asc()Returns the character code of the first character of a string.
Asc("A") returns 65.
Chr()Returns the display character of a character code.
Chr(65) returns "A".
GetChar()Returns the character at a specified position in a string, counting from 1.
GetChar("This is a string", 7) returns "s".
InStr()Returns the starting position in a string of a substring, counting from 1.
InStr("This is a string", "string") returns 11.
InStrRev()Returns the starting position in a string of a substring, searching from the end of the string.
InStr("This is a string", "string") returns 11.
LCase()Returns the lower-case conversion of a string.
LCase("THIS IS A STRING") returns "this is a string".
Left()Returns the left-most specified number of characters of a string.
Left("This is a string", 4) returns "This".
Len()Returns the length of a string.
Len("This is a string") returns 16.
LTrim()Removes any leading spaces from a string.
LTrim("   This is a string") returns "This is a string".
Mid()Returns a substring from a string, specified as the starting position (counting from 1) and the number of characters.
Mid("This is a string", 6, 4) returns "is a".
Replace()Replaces all occurrences of a sub-string in a string.
Replace("This is a string", " s", " longer s") returns "This are a longer string" (replaces an "s" preceded by a blank space).
Right()Returns the right-most specified number of characters of a string.
Right("This is a string", 6) returns "string".
RTrim()Removes any trailing spaces from a string.
RTrim("This is a string   ") returns "This is a string".
Str()Returns the string equivalent of a number.
Str(100) returns "100".
Space()Fills a string with a given number of spaces.
"This" & Space(5) & "string" returns "This     string".
StrComp()Compares two strings. Return values are 0 (strings are equal), 1 (first string has the greater value), or -1 (second string has the greater value) based on sorting sequence.
StrComp("This is a string", "This string") returns -1.
StrReverse()Reverses the characters in a string.
StrReverse("This is a string") returns "gnirts a si sihT".
Trim()Removes any leading and trailing spaces from a string.
Trim("   This is a string   ") returns "This is a string".
UCase()Returns the upper-case conversion of a string.
UCase("This is a string") returns "THIS IS A STRING".
Val()Converts a numeric expression to a number.
Val( (1 + 2 + 3)^2 ) returns 36.
The above summaries give you a basic idea of the uses of these arithmetic and string functions. There are occasions throughout these tutorials to see them in action and in combination as they are applied to various processing needs.

VB.Net SqlServer DateTime Insertion

Dealing with Date/Time in VB.Net and SQLServer

It's been a nightmare since i decided to insert DateTime value into Sql Server 2000 DataBase Table Field Type as SmallDateTime.It took me 3 days and nights trying to work it around.I've did everything i know and everything other told me to do.Finally, i figured it out.
Look ....
Say, you have a Vb.Net 2005 Project with a form contains a DateTimePicker Control called (Q3) and Button Called (B3) Text Property as (Insert), and you have Sql Server 2000 DataBase called (Market) with Table Called (Purchase) includes a Field called (EXdt) Type as (SmallDateTime) Length (4), and you want to update it using Ado.Net Online Mode.
1) Calling The Ado.Net for Sql Server DataBase
Imports System.Data.SqlClient
2) Assigning your variables
Dim CN As New SqlClient.SqlConnection
Dim cPur As New SqlClient.SqlCommand
Dim CntRs As Integer
Dim SqlStr As String
3) Connecting your DataBase 'Market' - Form_Load Event

If CN.State = ConnectionState.Open Then CN.Close()
        CN.ConnectionString = "Data Source=Your_Sql_Server_Name;Initial Catalog=Market;Integrated Security=True"
        CN.Open()
4) Connecting your table 'Purchase' - Form_Load Event

SqlStr = "Select * from Purchase"
        With cPur
            .Connection = CN
            .CommandType = CommandType.Text
            .CommandText = SqlStr
        End With
5) Inserting the date within the DateTimePicker Control (Q3) Into Field (EXdt)
SqlStr = "Insert Into Purchase (ExDT) Values ('" & Q3.Value.ToString("yyyy/MM/dd") & "')"
cPur.CommandText = SqlStr

CntRs = cPur.ExecuteNonQuery()
        MsgBox("( " & CntRs & " ) Record(s) Saved")
Congratulations ....
Download The Source Code Via MediaFire Link

If you have any other problem contact me by Comments - or -
you can simply Google me for more solutions (evry1falls - Your Problem)


Visual Basic Courses - Share