Manually programming a datagrid

frankm9639

Active member
Joined
Aug 24, 2006
Messages
25
Programming Experience
1-3
I want to manually program a datagrid as an exercise in teaching myself how to update, insert, and delete to a dataset and db. Is this a bad thing to do? Anyway, I wanted to ask a question. When you create a data adapter with the wizard it creates the sql statements for you. When I created a data adapter manually, I copied the wizard created lines and tried to modify them. But, I'm getting an error. Can you help?

---------code------------
SqlCommand.CommandText = "UPDATE COMPANY SET CompanyName = @CompanyName WHERE CompanyKey = @CompanyKey"
SqlDA_COMPANY.UpdateCommand = SqlCommand
SqlCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 50)
SqlCommand.Parameters.Add("@CompanyKey", SqlDbType.NVarChar, 5)

-----error------------
Prepared statement '(@CompanyName nvarchar(50),@CompanyKey nvarchar(5))UPDATE COMPAN' expects parameter @CompanyName, which was not supplied.

---------error 2----------
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Cannot create a child list for field COMPANY.
-------------------------
 

frankm9639

Active member
Joined
Aug 24, 2006
Messages
25
Programming Experience
1-3
Nevermind. I fixed it. Here is my solution for any readers who need this thread. Compare this code to the first posts' code:
----------------------------------------------------------------------
SqlCommandSelect.Connection = SqlConn

SqlCommandSelect.CommandText = "SELECT * FROM COMPANY"
SqlDA_COMPANY.SelectCommand = SqlCommandSelect


SqlCommandUpdate.Connection = SqlConn
SqlCommandUpdate.CommandText = "UPDATE COMPANY SET CompanyName = @CompanyName WHERE CompanyKey = @CompanyKey"
SqlCommandUpdate.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 50, "CompanyName")

SqlCommandUpdate.Parameters.Add("@CompanyKey", SqlDbType.NVarChar, 5, "CompanyKey")
SqlDA_COMPANY.UpdateCommand = SqlCommandUpdate
-------------------------------------------------------
I followed these links (found here of course):
http://www.vbdotnetforums.com/showthread.php?t=12501
http://builder.com.com/5100-6371_14-6093390.html
http://msdn2.microsoft.com/en-us/library/fxsa23t6(VS.80).aspx

 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I want to manually program a datagrid as an exercise in teaching myself how to update, insert, and delete to a dataset and db. Is this a bad thing to do?
Not necessarily, but sometimes it's like building a car to drive to the shops, when your neighbour's is available and he's said you can borrow it any time (because you bought it for him).

Anyway, I wanted to ask a question. When you create a data adapter with the wizard it creates the sql statements for you.
It guesses. It doesnt always get it right..

When I created a data adapter manually, I copied the wizard created lines and tried to modify them. But, I'm getting an error. Can you help?

When using sql commands with data sets and datatables, you need to tell the command from which table column it will get its value..

SqlCommandUpdate.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 50, "CompanyName")

The bold companyname refers to the name of the column in the datatable.. It is, of course, possible to have a datatable column called "CoNam" that is fillled from the companyname column in the database.. In this instance, the param add would be:

SqlCommandUpdate.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 50, "CoNam")

Just wanted to distinct the first and last elements there... To highlight that they arent necessarily related...
 
Top Bottom