Newbie: Need Help Please... :-(

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

All,

I'm pulling what little hair I have left on figuring out the concepts of DataSets, DataAdapters and what I find in all of the various books and samples I've accumulated of late.

Here's the Interface I'm trying to make work:

1. WinForm containing Grid and Buttons. Grid contains a selection of existing records in a table. If I double click on a record in the grid, I open another window showing all of the details for that specific record. (this form has ALOT of fields that need to be displayed, so screen real estate is at a premium)

2. If I click on a 'New' Button, then I open the same window without any data loaded with empty fields. I can then enter the new data, I click on the Save button, then I close the detail record and repopulate the grid on the calling form.

So, where I'm banging my head, is getting the the darn Insert & Update to work as I see NO changes making it to the database.

Question 1: For a 'New' record, it seems that using a Dataset is overkill as I should simply use a Command Object, load the parameters with the form values and simply do an ExecuteNonQuery. This seems like it would be faster and not have a lot of overhead. I've tried doing a dataset bit, but simply CAN'T get the InsertCommand associated to the DataAdapter to perform the Insert as it doesn't show up in the database. Frustrating and I feel I'm missing a key bit of understanding here.

Question 2: I'm using VS.Net 2003 and have gone through the various tutorials and so have gone through the process of using the wizard to generate Connections, DataAdapters and Datasets but all of these items are located in a section of code that says not to mess with it. Unfortunately, most of the books seem to show examples where they are generating most of the code by hand. What are most of you out there doing? The click and drag bit, or laying code out the old fashioned way? Sometimes, I feel like the IDE is doing so much for me, I'm just not understanding how things are really working under the hood. Not a good thing in my book.

Sorry this is so long, but I'm really stymied right now and desparately need to have some forward motion here.

Thanks to any that may extend a helping hand....Bobbo
 
Bobbo,

Since you did not provide any code samples, I am not exactly sure where you
are having trouble. But the two segments of code below work correctly, one
performs an UPDATE, and the other an INSERT, both using the
ExecuteNonQuery() method of the SqlCommand object. You are correct in
assuming that the better way to perform INSERTS and UPDATES is to use the
SqlCommand object with the ExecuteNonQuery() method. There is no need to
create a DataSet for these operations. As far as what is better practice
when writing your database applications, one, using a wizard to generate
your code, or two, as you put it, "Old School", and write it yourself. I do
not consider myself "Old School" as I am not in my thirties yet, but still
strongly believe that the better way to write efficient code is to write it
yourself and develop a mastery of the subject. Wizards tend to generate a
lot of code that sometimes is not needed for your specific needs, nor
understood without already having a mastery of the subject, but generated to
try and cover as many different situations that may arise. Therefore it
seems that the mastering the subject tends to be the common factor here.
Once you establish a good understanding of what you need to do, and how to
do it, you will be able to omit the unnecessary code that would normally be
generated by a wizard, as well as ease the debugging process when writing
your applications, as you will know exactly what is going on behind the
scenes.

Hope this helps,

Anthony

'///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
'// Code to perform UPDATE

Dim sqlCnxn As New SqlConnection("Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=myDB;Data Source=myServer;")
Dim sqlParam1 As New SqlParameter("@value1", SqlDbType.VarChar, 20)
Dim sqlParam2 As New SqlParameter("@value2", SqlDbType.VarChar, 55)
Dim sqlParam3 As New SqlParameter("@value3", SqlDbType.VarChar, 5)
Dim sqlParam4 As New SqlParameter("@value4", SqlDbType.Int)
Dim sqlCmd As New SqlCommand("UPDATE Product_Master SET P_Name=@value1,
P_Description=@value2, P_Version=@value3 WHERE UPID = @value4", sqlCnxn)

sqlCnxn.Open()

sqlParam1.Value = "New"
sqlParam2.Value = "New"
sqlParam3.Value = "New"
sqlParam4.Value = 3

sqlCmd.Parameters.Add(sqlParam1)
sqlCmd.Parameters.Add(sqlParam2)
sqlCmd.Parameters.Add(sqlParam3)
sqlCmd.Parameters.Add(sqlParam4)

sqlCmd.CommandType = CommandType.Text
sqlCmd.ExecuteNonQuery()

sqlCnxn.Close()

'///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////

'// Code to perform INSERT

Dim sqlCnxn As New SqlConnection("Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=myDB;Data Source=myServer;")
Dim sqlParam1 As New SqlParameter("@value1", SqlDbType.VarChar, 20)
Dim sqlParam2 As New SqlParameter("@value2", SqlDbType.VarChar, 55)
Dim sqlParam3 As New SqlParameter("@value3", SqlDbType.VarChar, 5)
Dim sqlCmd As New SqlCommand("INSERT INTO Product_Master (P_Name,
P_Description, P_Version) VALUES(@value1,@value2,@value3)", sqlCnxn)

sqlCnxn.Open()

sqlParam1.Value = "Test"
sqlParam2.Value = "Test"
sqlParam3.Value = "Test"

sqlCmd.Parameters.Add(sqlParam1)
sqlCmd.Parameters.Add(sqlParam2)
sqlCmd.Parameters.Add(sqlParam3)

sqlCmd.CommandType = CommandType.Text
sqlCmd.ExecuteNonQuery()

sqlCnxn.Close()
'///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////



Bobbo said:
All,

I'm pulling what little hair I have left on figuring out the concepts of
DataSets, DataAdapters and what I find in all of the various books and
samples I've accumulated of late.
Here's the Interface I'm trying to make work:

1. WinForm containing Grid and Buttons. Grid contains a selection of
existing records in a table. If I double click on a record in the grid, I
open another window showing all of the details for that specific record.
(this form has ALOT of fields that need to be displayed, so screen real
estate is at a premium)
2. If I click on a 'New' Button, then I open the same window without any
data loaded with empty fields. I can then enter the new data, I click on
the Save button, then I close the detail record and repopulate the grid on
the calling form.
So, where I'm banging my head, is getting the the darn Insert & Update to
work as I see NO changes making it to the database.
Question 1: For a 'New' record, it seems that using a Dataset is overkill
as I should simply use a Command Object, load the parameters with the form
values and simply do an ExecuteNonQuery. This seems like it would be faster
and not have a lot of overhead. I've tried doing a dataset bit, but simply
CAN'T get the InsertCommand associated to the DataAdapter to perform the
Insert as it doesn't show up in the database. Frustrating and I feel I'm
missing a key bit of understanding here.
Question 2: I'm using VS.Net 2003 and have gone through the various
tutorials and so have gone through the process of using the wizard to
generate Connections, DataAdapters and Datasets but all of these items are
located in a section of code that says not to mess with it. Unfortunately,
most of the books seem to show examples where they are generating most of
the code by hand. What are most of you out there doing? The click and drag
bit, or laying code out the old fashioned way? Sometimes, I feel like the
IDE is doing so much for me, I'm just not understanding how things are
really working under the hood. Not a good thing in my book.
Sorry this is so long, but I'm really stymied right now and desparately
need to have some forward motion here.
 
Back
Top