|
Hi Colin
i manage to Covert the Code to vb,but in other part will need your Help lets Start here
Public Class Dal
Dim connectionString As String
' <summary>
'The static initialiser will be called the first time any
' method on the class is called. This ensures that the
' connection string is available.
' </summary>
Shared Sub Dal()
DIm connectionString as String = ConfigurationManager.AppSettings["ConnectionString"]
End Sub
i had a problem with the Connection string
<br />
DIm connectionString as String = ConfigurationManager.AppSettings["ConnectionString"]<br />
The IDE tells me that "Identifier Expected"
The First one is the namespaces that needs to be imported
<br />
using System.Collections.Generic;<br />
This line it does not recognise the Generic. if i remove it "Generic" it does not Complain.
The Second Function i coverted it like this
Private Function BuildCommand(ByVal storedProcedureName As String)
' Create a connection to the database.
Dim connection = New SqlConnection(connectionString)
' Create the command object - The named stored procedure
' will be called when needed.
Dim result As New SqlCommand(storedProcedureName, connection)
result.CommandType = CommandType.StoredProcedure
Return result
End Function
i made it a Function because a sub cannot return a Value.
<br />
<br />
My Following Function is like this
' <summary>
' Builds a DataAdapter that can be used for retrieving the
' results of queries
' </summary>
' <param name="storedProcedureName">The name of the stored
' procedure</param>
' <returns>A data adapter</returns>
Private Function BuildBasicQuery(ByVal storedProcedureName As String)
Dim cmd As New SqlCommand(BuildCommand(storedProcedureName))
'Set up the data adapter to use the command already setup.
Dim result As New SqlDataAdapter(cmd)
Return result
End Function
it never Complained. and the Next one is like this
'<summary>
' A sample public method. There are no parameters, it simply
' calls a stored procedure that retrieves all the products
' </summary>
' <returns>A DataTable containing the product data</returns>
Public Function GetAllProducts()
Dim DataAdapter As New SqlDataAdapter(BuildBasicQuery("GetAllProducts"))
'Get the result set from the database and return it
Dim result As New DataTable
DataAdapter.Fill(result)
Return result
End Function
it never Complained too.
' <summary>
' A sample public method. It takes one parameter which is
'passed to the database.
' </summary>
' <param name="invoiceNumber">A number which identifies the
' invoice</param>
' <returns>A dataset containing the details of the required
' invoice</returns>
Public Function GetInvoice(ByVal invoiceNumber As Integer)
Dim DataAdapter As New SqlDataAdapter(BuildBasicQuery("GetInvoice"))
DataAdapter.SelectCommand.Parameters.AddWithValue( _
"@invoiceNumber", invoiceNumber)
Dim result As New DataSet
DataAdapter.Fill(result)
Return result
End Function
On this one i need your help, i got Stucked. the Code Complained in the Following line.
<br />
<br />
Dim DataAdapter As New SqlDataAdapter(BuildBasicQuery("GetInvoice" _<br />
, "@invoiceNumber", invoiceNumber))<br />
On the Last one, i was Completely lost
Public Function CreateInvoice(ByVal customerId As Integer, _
ByVal billingAddressId As Integer, ByVal DateTime As Date)
Dim cmd As New SqlCommand(BuildCommand("CreateInvoice"))
cmd.Parameters.AddWithValue("@customerId", customerId)
cmd.Parameters.AddWithValue("@billingAddressId")
billingAddressId)
cmd.Parameters.AddWithValue("@date",date)
cmd.Connection.Open()
dim result as Integer = (integer).cmd.ExecuteScalar()
cmd.Connection.Close()
Return result
End Function
The Compiler Complained about All the lines in this Function. the other thing is that i did not understand the Following line in the last function
<br />
int result = (int)cmd.ExecuteScalar()
i Think i you can confirm my Code, i will try to understand.
Thanks again Colin
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sudden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
|
|
|
|
|
Vuyiswamb wrote: On this one i need your help, i got Stucked. the Code Complained in the Following line.
Dim DataAdapter As New SqlDataAdapter(BuildBasicQuery("GetInvoice" _
, "@invoiceNumber", invoiceNumber))
BuildBasicQuery returns a SqlDataAdapter already, so passing it as an argument to the constructor of SqlDataAdapter is likey to cause problems. You don't need to call the constructor again as you already have an instance of the object.
So the line of code should be something like this:
Dim dataAdapter As SqlDataAdapter = BuildBasicQuery("GetInvoice")
Also note that BuildBasicQuery only takes one parameter, the name of the stored procedure. You add the parameters afterwards.
Vuyiswamb wrote: The Compiler Complained about All the lines in this Function.
VB is not supposed to be case sensitive, but that is because the IDE sorts that out for you. Perhaps that is the problem. dim should be Dim , etc.
Vuyiswamb wrote: Dim cmd As New SqlCommand(BuildCommand("CreateInvoice"))
Again, BuildCommand returns a command object already. No need to create a new one.
Vuyiswamb wrote: cmd.Parameters.AddWithValue("@billingAddressId") billingAddressId)
You have closed the bracket too early. Replace it with a comma and underscore to allow the code to progress to the next line.
Vuyiswamb wrote: int result = (int)cmd.ExecuteScalar()
i Think i you can confirm my Code, i will try to understand.
Now, this is one thing I don't know how to do in VB. (int) is the casting operator. In this case it is casting to an integer. ExecuteScalar returns an object, so it can be any type that is being returned. The code requires it to be an integer, so that is what it must be cast to.
I hope this helps.
I also hope that you are looking at the sematics (the meaning of the code) as well as the syntax (the grammar). So far you seem to have been concentrating on the syntax - which may be fine if it is easier for you to understand in VB.NET.
|
|
|
|
|
Thanks Colin and Dave. as you know am still learning how to use Dal. well as you said am a bit clumsy on my code. sorry for that. Colin am Trying convert your code from your Blog from C# to vb.net 2003.
public int CreateInvoice(int customerId, int billingAddressId,
DateTime date)
{
SqlCommand cmd = BuildCommand("CreateInvoice");
cmd.Parameters.AddWithValue("@customerId", customerId);
cmd.Parameters.AddWithValue("@billingAddressId",
billingAddressId);
cmd.Parameters.AddWithValue("@date", date);
cmd.Connection.Open();
int result = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
return result;
}
}
Am trying to covert this code. amd when its in Vb, i will find my way around.
Thanks Again Colin And Dave, this is what will make me learn and be like you. if you have any Comments on how to increase my Programming skills i will apreciate it.
Thanks
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sudden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
|
|
|
|
|
Vuyiswamb wrote: i had a problem with the Connection string
DIm connectionString as String = ConfigurationManager.AppSettings["ConnectionString"]
The IDE tells me that "Identifier Expected"
The First one is the namespaces that needs to be imported
This needs it's namespace imported at the top of the code.
Imports System.Configuration
Vuyiswamb wrote: using System.Collections.Generic;
This line it does not recognise the Generic. if i remove it "Generic" it does not Complain.
What version of VB.NET are you using? 2003, 2005, ??? Generics only exist in 2005 (.NET 2.0) and above.
Vuyiswamb wrote: The Second Function i coverted it like this
Should be this instead:
Private Function BuildCommand(ByVal storedProcedureName As String)
' Create a connection to the database.
Dim connection As New SqlConnection(connectionString)
' Create the command object - The named stored procedure
' will be called when needed.
Dim result As New SqlCommand(storedProcedureName, connection)
result.CommandType = CommandType.StoredProcedure
Return result
End Function
Vuyiswamb wrote: My Following Function is like this
Should be this becuase BuildCommand already returns an SqlCommand object:
' Builds a DataAdapter that can be used for retrieving the
' results of queries
Private Function BuildBasicQuery(ByVal storedProcedureName As String)
Dim cmd As SqlCommand = BuildCommand(storedProcedureName)
' Set up the data adapter to use the command already setup.
Dim result As New SqlDataAdapter(cmd)
Return result
End Function
That's enough for now. Guy, the very simple mistakes you put into your version of the code have nothing to do with converting it from C# to VB.NET. They stem more from not understanding the VB.NET you're writing. You're converting the code visually, but you're not understanding the concepts that code is illustrating.
|
|
|
|
|
Vuyiswamb wrote: On the Last one, i was Completely lost
Public Function CreateInvoice(ByVal customerId As Integer, _ ByVal billingAddressId As Integer, ByVal DateTime As Date) Dim cmd As New SqlCommand(BuildCommand("CreateInvoice")) cmd.Parameters.AddWithValue("@customerId", customerId) cmd.Parameters.AddWithValue("@billingAddressId") billingAddressId) cmd.Parameters.AddWithValue("@date",date) cmd.Connection.Open() dim result as Integer = (integer).cmd.ExecuteScalar() cmd.Connection.Close() Return result End Function
The Compiler Complained about All the lines in this Function. the other thing is that i did not understand the Following line in the last function
This one just has errors all over it. The function header was declared wrong at the Date parameter, and you also didn't give it a return type. Calling BuildCommand returns an SqlCommand object, which you're passing to the constructor of another SqlCommand object, which won't work. The third mistake was trying to split a statement into multiple lines improperly, thus giving the AddWithValue method one parameter instead of the two it expected. The function parameter list being wrong automatically made the forth mistake because you were trying to use "Date" which wasn't defined. The fifth mistake is with the cast of the return Object from ExecuteScalar to an Integer. You're trying to do it using syntax that is, well, I have no idea what you were doing...
Public Function CreateInvoice(ByVal customerId As Integer, _
ByVal billingAddressId As Integer, ByVal ByVal Date As DateTime) As Integer
Dim cmd As SqlCommand = BuildCommand("CreateInvoice")
cmd.Parameters.AddWithValue("@customerId", customerId)
cmd.Parameters.AddWithValue("@billingAddressId", billingAddressId)
cmd.Parameters.AddWithValue("@date", date)
cmd.Connection.Open()
Dim result as Integer = CInt(cmd.ExecuteScalar())
cmd.Connection.Close()
Return result
End Function
In order to convert code from one language to another, you at least have to have a firm grasp of the language you're convert it TO, not necessarily the one your converting it FROM. You really need to learn the basics of VB.NET before you try and convert code from another language. If you can't write proper code in VB.NET, you're just going to write code that generates hundreds of errors where you have no idea what caused them.
|
|
|
|
|
Thank you very much
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sudden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
|
|
|
|
|
I am learning visual basics. I wrote two simple seperate projects from tutorials and want to know how how I can launch them from each other.
Thanks
|
|
|
|
|
Process.Start is how you start another program, in code.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi All,
Could someone help me understand how to pull data stored in an XML file into my Excel spreadsheet?
I have tried to treat the file as though it were text, and wrote a routine to parse it all, but it is very cumbersome to use.
There must be a better way!
Albert Giraudi
|
|
|
|
|
Hi Albert,
There are various ways to read an XML. Some of the standard ways which you can use are –
1. Use the various custom controls available in the market (they are ready to use and easy too).
2. Use the XMLTextReader class which is derived from XMLReader.
Here is some basic code which can be utilized in your application –
*************************Code Part*************************
Imports System.
Imports System.Xml
' Open an XML file.
Dim textValue as String
Dim localName as String
Dim textReader As XmlTextReader = New
XmlTextReader (My.applicatin.Info.DirectoryPath & "\xmltest.xml").
textReader.Read()
If textReader.HasValue Then
' Move to fist element
textReader.MoveToElement()
localName=textReader.LocalName
textValue=textReader.Value.ToString()
‘ You can put your custom code to write the value to an Excel file from here onwards.
*************************End of Code*************************
Albert hope this helps you to get your application up and running . Have a nice day.
Regards,
Gary Bigman.
Software Engineer
ComponentOne LLC
www.componentone.com
|
|
|
|
|
|
Hello,
I want to show a loginform after the mainform is completely loaded. If I place the code in the Form_load event then the loginform is shown before the mainform is visible.
Where do I put the code so that the mainform is visible before the loginform is loaded (visible)
Thanks
|
|
|
|
|
I have not used it myself yet, but it seems .NET 2.0 introduced a Shown event for
this purpose...
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
|
2 possibilities not very elegants but work:
1) you can insert a timer at the end of the load form that will open the loginform
2) in load form event:
Show()
Login.ShowDialog()
Hope it helps
Shay Noy
|
|
|
|
|
2 posibility's:
shown event
activated event (not entiraly shure about this one)
If my help was helpfull let me know, if not let me know why.
The only way we learn is by making mistaks.
|
|
|
|
|
|
I am developing a test application at work, the item being tested operates with a serial comm port, and a network port. I am looking for either VB 6 objects or VB.net 2003 objects to interact with the device. I need to send data strings to it, recieve data strings, change communication settings and speeds. I would appreciate any and all objects and any code samples anyone could provide.
Phill Goin
|
|
|
|
|
Hi Phill,
There is some nice code and steps given in the MS KB which you can access from the given link below –
http://support.microsoft.com/kb/823179
The steps can be used for creating application to access Ports (whether Serial and Parallel).
Have a nice day.
Regards,
Gary Bigman.
Software Engineer
ComponentOne LLC
www.componentone.com
|
|
|
|
|
Thank you for the Information, I am so far successful with the comm port part of the code, it the network access to a device plugged in directly thru a cross over cable that is making my life difficult.
Any information on communicating on that port would be appreciated.
Thank you for the quick response.
Phill Goin
|
|
|
|
|
It's no different than any other TCP/IP based network. You have but two devices, each still requires it's own IP address on the same subnet. If the NIC that the device is plugged into has an IP of 192.168.0.x and the device is on 10.x.x.x, it's not going to work. Both NIC's have to be on the same subnet.
Other than that, it's going to be standard Sockets based communication. "vb6 sockets"[^]
|
|
|
|
|
Thank you for your assistance, I am able to set the IP addresses of both the machine and device, but it is the connecting, data transfer and interaction objects that I have no idea what to use. I would prefer VB.net 2003 but VB6 or VB.net 2005 will work in a pinch.
Thanks all
Phill Goin
|
|
|
|
|
Hi i'm using an access database. My dataset and table adapters are all generated by the "add datasource" wizard and hence all my select, insert, delete and update commands too. If i add a record to a table it inserts fine and i can update it and save. as soon as I add another record and edit the various fields in my datagridview and save, an error occurs (which is the same as the title of this message). i am working in a single user environment and using Vs 2005. Is there something I need to check for? I tried turning off optimistic concurrency but it still doesn't work.
Mr Oizo
|
|
|
|
|
This means that your SQL statement didn't update the number of rows in the database that the DataAdapter/TableAdapter expected it to. If you're using entirely designer generated code, this is usually caused by a starting SQL SELECT statement that is selecting data from more than a single table in your database.
The Adapters only work with SQL that selects and modifies a single table. You can use an Adapter to SELECT from multiple tables, but the resulting code for the UPDATE, DELETE, and INSERT will all fail if autogenerated. The autogenerated code uses the SELECT statement to generate the remaining SQL statements, but it cannot cope with a SELECT that retrieves data from more than one table.
|
|
|
|
|
Hi. Thanks for the reply. I think ill delete my dataset and adapters and regenerate them. I think it could have son=mething to do with the fact that my coulumn in the database named "AbsoluteLength"(which returns the absolute path of the file) is only 50 characters long. I changed it in the database to 200 characters just for safe keeping and it still shows 50 characters for max length for of that field(according to the apapter) . thanks
Beric
Mr Oizo
|
|
|
|
|