|
Read[^] for a better explanation of what you're doing, complete with a working sample.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I am new to VB.Net and need some help creating a simple project.
I am tryint to user select and insert queries to make it work but somehow nothing happens when i click on the button.
Here is the code I am using to do so.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & ("C:\db1.mdb")
End Sub
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim searchcommand1 As String = "SELECT * FROM Change Control WHERE (Log number = 'txtlognum')"
Private Sub btnsave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & ("C:\dblog.mdb")
Dim InsertCommand1 As String = " INSERT INTO Change Control VALUES ('txtlognum','txtdtrqst','txttrgdt','txttime','txtrqstby','txtprojname','txtdscp','txtusers','txtdpt')"
MsgBox("New entry has been added", vbOKOnly + vbExclamation, "Success")
----------------------------------------------------------------
On the left column under Server Explorere I can see all the a tables in my database which means I can connect to it.
Can somebody please guide me. I don't know what am I doing wrong.
Thanks.
|
|
|
|
|
Well, your Select and Insert commands are going to choke because you can't have a space in a table or columns names without enclosing them in square brackets. You're not passing the values of your textboxes correctly either. Drop the code in the Form_Load event. It's useless.
You're not even executing these SQL commands! You put together a SQL statement in a string, define a connection string, but then you don't use them to do any work! Of course nothing is going to happen!
Also, DON'T concatenate strings together like this to build SQL statements. It's just bad practice and leads to difficult to find errors, and opens your code up for SQL Injection attacks.
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim searchcommand1 As String = "SELECT * FROM [Change Control] WHERE ([Log number] = " & txtLognum.Text
' You're missing the code to actually execute this statement against the database.
' You're also missing code to do something with the returned data, like display it in
' a datagrid!
End Sub
Private Sub btnsave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\dblog.mdb"
Dim InsertCommand1 As String = "INSERT INTO [Change Control] VALUES " & _
"'" & txtlognum.Text & "','" & txttrgdt.Text & "','" & txttime.Text &_
"','" & txtrqstby.Text & "','" & txtprojname.Text & "','" & txtdscp.Text & _
"','" & txtusers.Text & "','" & txtdpt.Text & "')"
' You're missing the code to actually execute this statement against the database.
MsgBox("New entry has been added", vbOKOnly + vbExclamation, "Success")
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
You are so AWSOME!!
Thanks for your help. I will try this but I am sure I will have more questions as I start getting the hang of it. Some how MSDN did not help.
Shahid.;)
|
|
|
|
|
That will work, I would write it diffrently:
Public Function SQLSafe(str as String)
Return str.replace("'", "''")
End Function
Private Sub btnsave_Click_1(......) Handles btnsave.Click
Dim Cmd as String = "INSERT INTO [Change Control] VALUES ('{0}','{1}','{2}','{3}','{4}',{5}','{6}','{7}','{8}')"
Dim Exec = String.Format(Cmd, SQLSafe(txtlognum.Text), SQLSafe(txttrgdt.Text), SQLSafe(txttime.Text), _
SQLSafe(txtrqstby.Text), SQLSafe(txtprojname.Text), SQLSafe(txtdscp.Text), SQLSafe(txtusers.Text), SQLSafe(txtdpt.Text))
End Sub
Matthew Hazlett
|
|
|
|
|
I'd modify it even further by scraping it and using parameterized stored procedures. I can't stand building SQL statements with strings and try to avoid it using all possible methods.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks guys.
Actually that dotnetspider.com was helpfull but I now I am really stuck. I downloaded a sample application from there and helped me build most of my code. I created a bunch of functions within one big class. Now when I try to use the Insert query I get error msgs (Value of type 'String' cannot be converted to 'WindowsApplication1.Change'). Offcourse I am doing a lot of things wrong but I guess I am dumb enough to not know where the problem is.
Here is the code for the SAVE button.
Private Sub btnsave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click<br />
<br />
<br />
Dim ad As New Change<br />
<br />
txtlognum.Text = ad.LogNumber<br />
txtdtrqst.Text = ad.DateRequested<br />
txttrgdt.Text = ad.TargetDate<br />
<br />
ad.LogNumber = Change.Createdata("Change", "LogNumber")<br />
ad.DateRequested = Change.Createdata(txtdtrqst.Text)<br />
ad.TargetDate = Change.Createdata(txttrgdt.Text)<br />
ad.Time = Change.Createdata(txttime.Text)<br />
ad.RequestedBy = Change.Createdata(txtrqstby.Text)<br />
ad.ProjectName = Change.Createdata(txtprojname.Text)<br />
ad.ChangeDescription = Change.Createdata(txtdscp.Text)<br />
ad.Users = Change.Createdata(txtusers.Text)<br />
ad.Department = Change.Createdata(txtdpt.Text)<br />
<br />
<br />
MsgBox("New entry has been added", vbOKOnly + vbExclamation, "Success") ---------------------------------------------------------------------
Here is my Class
Public Class Change<br />
<br />
Public CategoryHardware As CheckBox<br />
Public categorySoftware As CheckBox<br />
Public CategoryNetwork As CheckBox<br />
Public CategoryDatabase As CheckBox<br />
Public CategoryTcom As CheckBox<br />
Public CagegoryFacilities As CheckBox<br />
Public RiskHigh As CheckBox<br />
Public RiskMedium As CheckBox<br />
Public RisLow As CheckBox<br />
Public NewProgram As CheckBox<br />
Public Maintnace As CheckBox<br />
Public Problem As CheckBox<br />
Public Enhancement As CheckBox<br />
Public Other As CheckBox<br />
<br />
Public LogNumber As Int16<br />
Public DateRequested As DateTime = Nothing<br />
Public TargetDate As DateTime = Nothing<br />
Public Time As String<br />
Public RequestedBy As String<br />
Public ProjectName As String<br />
Public ChangeDescription As String<br />
Public Users As String<br />
Public Department As String<br />
<br />
Public TypeEr As String<br />
Public TypeException As String<br />
Public TypeNormal As String<br />
<br />
Public Description As String<br />
Public Completed As CheckBox<br />
Public Initials As String<br />
Public Duration As String<br />
<br />
Public Shared Function Getdata(ByVal LogNumber As System.Int16) As Change<br />
Dim searchcommand1 As String = "SELECT * FROM [Change Control] WHERE ([LogNumber] = " & LogNumber<br />
<br />
'Dim searchcommand2 As String = "SELECT * FROM Change Type"<br />
'Dim searchcommand3 As String = "SELECT * FROM Change Category"<br />
<br />
End Function<br />
<br />
Public Shared Function Createdata(ByVal addobj As Change)<br />
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _<br />
& "Data Source=" & ("C:\db1.mdb")<br />
<br />
Dim InsertCommand1 As String = "INSERT INTO Change Control (LogNumber, DateRequested, TargetDate, Time, RequestedBy, ProjectName, ChangeDescription, Users, Department) VALUES (" & addobj.LogNumber & ", " & addobj.DateRequested & ", " & addobj.TargetDate & ", " & addobj.Time & ", " & addobj.RequestedBy & ", " & addobj.ProjectName & ", " & addobj.ChangeDescription & ", " & addobj.Users & ", " & addobj.Department & ")"<br />
<br />
End Class
Any help would be appreciated.
-- modified at 11:32 Wednesday 4th January, 2006
|
|
|
|
|
I would HIGHLY suggest dropping this for now and picking up a book on VB.NET for Beginners. You've got a TON of misconceptions floating around this code and almost as much code MISSING.
You still don't have any code in here to actually read/write anything to the database! I already posted a link to something that will show you the basics of database access. READ IT[^]!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I guess you are right. I will dig more on that website to get help.
Thanks.
|
|
|
|
|
From what I've seen in your code, you really need to go through some tutorials to show you the basics of what your doing.
Try this[^].
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I am doing a VB.net tutorial and they ask me to pull in a data reader from the toolbox.
I don't see a data reader in the toolbox?
I assume it has something to do with the sqldataadapter?
Where do I load this datareader?
|
|
|
|
|
I get the feeling that your going through a tutorial written for VB.NET 2003 and your using VB.NET 2005, right?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
First, you have to be editing the form in the designer, not the code.
Then, if you open the ToolBox, find the Data section (tab). It'll be in there.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Here is what I see in the Data tab of the tool box:
Pointer
Dataset
SqlDataAdapter
SqlConnection
SqlCommand
Dataview
the other stuff is for OLE and Oracle
There is no object that says dataReader
|
|
|
|
|
Sorry, I don't have 2003 in front of me anymore.
You might see OleDbDataReader instead. The organization of all these classes is pretty simple.
Anything that starts with Sql...... is for a database stored on a MS Sql Server.
Anything that starts with OleDb.... is for a database stored in a Jet database, otherwise known as MS Access.
If you're using an OleDbConnection object, use an OleDbDataReader to go with it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
kenn_rosie wrote: I am doing a VB.net tutorial and they ask me to pull in a data reader from the toolbox.
Don't tutorials teach people how to actually write code anymore.
A Data Readers are just classes. You can get different types of data reader depending on your database or you can get some generic types like OdbcDataReader[^]or OleDbDataReader[^] or specific types like SqlDataReader[^] or OracleDataReader[^]
kenn_rosie wrote: I assume it has something to do with the sqldataadapter?
The data adapter classes use their data reader counterpart in order to extract the information from the database. All data extraction from the database comes in through a data reader. The Data Adapter justs adds an addional layer of abstraction away from the database in order to hide the details from the implementor (i.e. the developer)
kenn_rosie wrote: Where do I load this datareader?
For example (assuming you already have a command object called myCommand):
Dim myDataReader As OleDbDataReader
myDataReader = myCommand.ExecuteDataReader()
Does this help?
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
I am still confused.... do I need to open the SqlDataAdapter from the tool box and the dataReader is part of that?
Probably sounds silly to you but this is my first shot at VB.net
|
|
|
|
|
kenn_rosie wrote: do I need to open the SqlDataAdapter from the tool box and the dataReader is part of that?
While the SqlDataAdapter uses an SqlDataReader internally, you never get to see it. What happens is that the data adapter:
Opens the connection
Executes the command getting a data reader
Uses the data reader to build the DataSet
Closes the Data Reader
Closes the connection (but, only if it was closed to start with)
Returns the DataSet
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Maybe this will help see what I mhave done so far:
I placed a DataGrid on an ASP.NET Web page added the following code:
<asp:datagrid runat="server" id="KennDataGrid">
I placed the code in the HTML form as follows:
<asp:datagrid runat="server" id="KennDataGrid">
Here the id="KennDataGrid will be the name of the DataGrid used when referring to it in our server-side code.
I know I needed to bind data to the DataGrid.
The tutorial said:T
The first thing we need to do is to grab a DataReader containing some database data.
This is all I have done on the project so far. Where I am lost is "grabbing this datareader"they are talking about
|
|
|
|
|
kenn_rosie wrote: Where I am lost is "grabbing this datareader"they are talking about
Probably just a very informal way of saying use a data reader - but I'm surprised they don't show you what they mean.
Unfortunately, I cannot help with binding the data to the data grid - I didn't like them in .NET 1.1 (I felt they promoted poor coding practices by too tightly coupling the data layer to the GUI layer). They are much improved in .NET 2.0 but I haven't had a chance to look at them.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
I want to get the system contextmenu of any type files when i right click this type file in listview control in vb.net,the effect is like you right click the file in Windows folder,but i cann't do that.
Anyone can help me?
|
|
|
|
|
hi,
glitch occurs when i try drag that dialog to another when its subform is running. simulation is
1. run the program , click the command buton in form 1
2. click ok button in dialog
3. quickly drag the dialog, u can see the replica of its own image thro' the place whereever u drag until that loop for its subforms runs after that it is normal.
can anybody give ideas to solve this problem.
www.tamilselvan.zoomshare.com
|
|
|
|
|
dialog
------
------
Private Sub Form_Load()
End Sub
Private Sub OKButton_Click()
Load Form2
Form2.Show vbModeless
'DoEvents
For i = 1 To 300
For j = 1 To 1000
'DoEvents
Dialog.AutoRedraw = True
Form2.Caption = "hello"
Next j
Next i
End Sub
form 1
------
------
Private Sub Command1_Click()
Load Dialog
'Me.Hide
Dialog.Show vbModeless
'DoEvents
dodummy
End Sub
Private Sub dodummy()
End Sub
form 2
------
------
Private Sub Form_Activate()
fill
End Sub
Private Sub fill()
For i = 1 To 100
For j = 1 To 1000
Form2.FillColor = 0
Next j
Next i
End Sub
www.tamilselvan.zoomshare.com
|
|
|
|
|
There is no solution to this problem. Your form2 won't redraw itself until that Fill loop in it is done.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|