|
As far as I can tell, it should work if you change your code a little bit:
<br />
tam.AcceptChanges() 'try this<br />
table.AcceptChanges() 'or try this, or try to use both lines, but before the update command<br />
dataAdapter.Update(temp)<br />
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
I've edited my code as you said but it didn't work. Here is the error message
System.Data.DatarowInaccessibleException: deleted row information cannot be accessed through the row
|
|
|
|
|
Please test this:
<br />
Private Sub ButtonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdate.Click<br />
dataConnection.Open()<br />
Dim temp As DataTable<br />
Try<br />
Dim r As DataRow<br />
For Each r In table.Rows<br />
If IsDBNull(r.Item("classID")) = True Then<br />
r.Item("classID") = TextboxClassID.Text<br />
End If<br />
Next<br />
table.AcceptChanges()<br />
temp = table.GetChanges() 'table is a dataTable which is used for datagrid source<br />
If Not temp Is Nothing Then dataAdapter.Update(temp)<br />
Catch ex As Exception<br />
table.RejectChanges()<br />
dataConnection.Close()<br />
MsgBox(ex.ToString)<br />
End Try<br />
End If<br />
dataConnection.Close()<br />
End Sub<br />
If that also doesn't work, I will try to have another look at it tomorrow.
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
It didn't work. Here is the error message
System.Data.DatarowInaccessibleException: deleted row information cannot be accessed through the row
at the line "If IsDBNull(r.Item("classID")) = True Then"
|
|
|
|
|
How about this:
<br />
Private Sub ButtonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdate.Click<br />
dataConnection.Open()<br />
Dim temp As DataTable<br />
Try<br />
table.AcceptChanges()<br />
Dim r As DataRow<br />
For Each r In table.Rows<br />
If IsDBNull(r.Item("classID")) = True Then<br />
r.Item("classID") = TextboxClassID.Text<br />
End If<br />
Next<br />
table.AcceptChanges()<br />
temp = table.GetChanges() 'table is a dataTable which is used for datagrid source<br />
If Not temp Is Nothing Then dataAdapter.Update(temp)<br />
Catch ex As Exception<br />
table.RejectChanges()<br />
dataConnection.Close()<br />
MsgBox(ex.ToString)<br />
End Try<br />
End If<br />
dataConnection.Close()<br />
End Sub<br />
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
It didn't work. No error message but when I ran debug, I found this :
temp is nothing
And dataAdapter didn't update anything.
|
|
|
|
|
The problem is that the IsDBNull check can not access rows where the RowState is Deleted (so we tried to call AcceptChanges first, but your update statement needs to know the RowState of each datarow to work, and AcceptChanges sets all RowStates to Unchanged. That's why the code didn't throw any errors, but also didn't update anything, because table.GetChanges() didn't find any changes anymore.
However, if the row was deleted than we don't have to add the ClassID anyway. So let's try the following:
<br />
Private Sub ButtonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdate.Click<br />
dataConnection.Open()<br />
Dim temp As DataTable<br />
Try<br />
Dim r As DataRow<br />
For Each r In table.Rows<br />
If r.RowState <> DataRowState.Deleted Then<br />
If IsDBNull(r.Item("classID")) = True Then r.Item("classID") = TextboxClassID.Text<br />
End If<br />
Next<br />
temp = table.GetChanges() 'table is a dataTable which is used for datagrid source<br />
If Not temp Is Nothing Then dataAdapter.Update(temp)<br />
table.AcceptChanges() 'this may not be necessary<br />
Catch ex As Exception<br />
table.RejectChanges()<br />
dataConnection.Close()<br />
MsgBox(ex.ToString)<br />
End Try<br />
End If<br />
dataConnection.Close()<br />
End Sub<br />
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
It worked finally ! And the last question. In dataAdapter I used "select * from student" and I filled them in datatable which names Table. Table is used for datagrid's source. But I don't want to display classID column in datagrid, what should I do ? Thank you very much for your help so far !
|
|
|
|
|
No problem, I am glad to hear it works.
If you want to filter out certain columns from view, you will have to use the Data Grid's TableStyles property (form designer > datagrid's properties > 4th from the bottom usually).
With this property you can put a "mask" between the table and the datagrid. Just add a table to the TableStyle and use its MappingName property to map it to your table.
To the TableStyle table you just created, you add GridColumnStyles (usually 3rd from the bottom). Just add a column here for each column you want to show, and map each column to the appropriate column in your table.
With GridColumnStyles, if you like, you can then also determine column width, alternating row color, etc.
Good luck,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
Thanks again ! Could you write commandline like this
dim TableStyle as new datagridTableStyles
etc.
I have read the example in MSDN. But I couldn't write the code.
|
|
|
|
|
It is indeed possible to make these TableStyles, etc, at runtime, but it is just far easier to do it in the designer. For the same reason I have no experience with implementing TableStyles at runtime myself.
If you really must do it that way, I suggest that you experiment a little bit, and if necessary, post an entirely new question on this board. Maybe someone else can help you with it.
Good luck,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
I have made it work on my own. But thank you anyway ! I've just read your signature and I think I'm a lucky man. I'm just a university student and I have nothing to pay you . Would you like to make friend with me ? Here is my email cotinhkhongtin@yahoo.com .
|
|
|
|
|
I got new problem here. StudentID is the primary key of the datatable Student. I don't want to display this column on datagrid. When I finish editing a row and click to other row on datagrid, error mesage appears "StudentID is not allowed null". I use a function name CreateKey to create the value of StudentID. I put the function CreateKey on the CurrentCellChanges event but it didn't work. Can you help me ?
-- modified at 10:22 Wednesday 14th February, 2007
|
|
|
|
|
Hi Bircut,
I have a feeling that maybe you need to look at the structure of the application, and not so much at where to place your function. What I mean is this:
Let's say that after you have created your Classroom ID, you want to add students to the classroom.
First you need to create the students in their own table. We can safely assume that this will be done by an administrator, who is allowed to see the Student ID (you don't have to hide it here). So you make a form in which the admin can add, delete and change students (all the students in the entire school). Your CreateKey function can be implemented here somewhere, because you want a unique ID for each student.
Next you can make an 'Add Student' Button and a ComboBox (for example) that you bind to the Students table. From this ComboBox you can then select a student from the Student Table, and add it to the Classroom Table with the Button. Something along the lines of:
<br />
'Button_Click Event:<br />
'Determine correct record from Student table based on Student name (for example)<br />
Dim StudentName as String<br />
StudentName = ComboBox.SelectedItem<br />
SQL_string = "Select * From Students Where Students.StudentName = '" & StudentName & "'"<br />
To the DataGrid that shows the students in the classroom, you then add a TableStyle, and GridColumnStyles only for the columns you want to show.
Good luck,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
I undertood your method and I had thought about this before. But my professor didn't agree. I also thought about using autonumber type to make the value. But I tried to use the function CreateKey because the character in StudentID have their meaning, I want to manage them. But I don't have time to think about it anymore. I need to finish this project before March. So I let the Administrator chose the value of StudentID finally. And one more thing, my project didn't mention about Student and Class. I tried to make thing easy to understand and someone could help me. Thanks again !!!
|
|
|
|
|
Sorry Guys/Ladies... this is a really basic question and you will probably scoff and laugh... I haven’t done any coding in 10 years much less any in VB.Net.
My question is related to a form I have built to return values from a directory. I need the box populated with the file names from a given directory and then populate the second combo box with the selected field in the first combo box...
Sorry for such a basic question, but if possible please help??
|
|
|
|
|
System.IO.Directory.GetAllFiles ( from memory ) will return the filenames. You can use an array as a data source for a control.
The combo has a selected index changed event, you can use this to pass data to the second combo.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi All,
Is there a way to display a form kinda frameless (without the blue bar going across the top and sides making is look like a new window and saying form2 on it)
Thanks in advance
Jaidev
|
|
|
|
|
Yes, set the FormBorder property ( from memory ) to none ( again, from memory ). It's along those lines.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I know the way to listing or enumerate sql server from my lan...
But is possible to listing or enumerate DB2 Server via VB or VBA???
|
|
|
|
|
How do you get a program to start @ windows start-up?
|
|
|
|
|
You would have to create a registry value in the HKCU\Software\Microsoft\Windows\CurrentVersion\Run key. See here for more information.
Trinity: Neo... nobody has ever done this before.
Neo: That's why it's going to work.
|
|
|
|
|
Or you can copy/move your program to the startup folder and it will start up as soon as you logon.
To access this folder you press start>> then
Programs>>Then startup>> then right click select open or explore.
With the registry keys you can use:
Local machine(Which starts the program for all users)
hklm/software/microsft/windows/run
Current user(Which starts the program for only the currently logged on user)
hkcu/software/microsoft/windows/run
Hope this helps!
|
|
|
|
|
Thanks for your replys....
|
|
|
|
|
I can see that I can use this to create a new tab page
But how can I make the Tab Page public?
TabControl1.Controls.Add(New TabPage("New Tab")))
Thanks
|
|
|
|