Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview not finding values for update. 1

Status
Not open for further replies.

smbrown

Programmer
Mar 30, 2009
15
US
I have a web form that users can select dropdowns on, and fill a grid with the returned values. I need the grid to allow the user to make changes to the grid, and have those changes goto the DB. There is an error on the myID, myUser1... in the VB. It can't find the values for these. Do I need to Dim these, or what? Not sure what I am missing to get this to run? I checked out some other posts, and am not having any luck.

Found this post and tryed to use this as the basis for what I have thread855-1523389 but now I am stuck!

<asp:GridView
ID="GridView1"
runat="server"
Font-Size="X-Small"
Font-Names="Arial"
AutoGenerateColumns="True"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDataBound="GridView1_RowDataBound"
BorderColor="Black">
<Columns>
<asp:commandfield ShowSelectButton="True" ShowEditButton="True">
</asp:commandfield>
<asp:boundfield DataField="ID" SortExpression="myID" HeaderText="myID">
</asp:boundfield>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/MYDB.mdb"
SelectCommand="SELECT * FROM [Test]" >
<UpdateParameters>
<asp:controlparameter Type="String" Name="myID" ControlID="gridview1" PropertyName="selectedrow.cells(0).text"/>
<asp:controlparameter Type="String" Name="myUser1" ControlID="gridview1" PropertyName="selectedrow.cells(1).text"/>
<asp:controlparameter Type="String" Name="myTime1" ControlID="gridview1" PropertyName="selectedrow.cells(2).text"/>
<asp:controlparameter Type="String" Name="myComments" ControlID="gridview1" PropertyName="selectedrow.cells(3).text"/>
<asp:controlparameter Type="String" Name="myWorkOrder" ControlID="gridview1" PropertyName="selectedrow.cells(4).text"/>
</UpdateParameters>
</asp:AccessDataSource>

Dim sql As String = "UPDATE Test SET ID = ?, User1=?, Time1=?, WorkOrder=?, Comments=? WHERE ID=?"
Using cnn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\Project1\App_Data\MYDB.mdb")
Using cmd As New OleDbCommand(sql, cnn)
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@ID", ID)
cmd.Parameters.AddWithValue("@User1", UserName)
cmd.Parameters.AddWithValue("@Time1", Time1)
cmd.Parameters.AddWithValue("@Comments", Comments)
cmd.Parameters.AddWithValue("@WorkOrder", WorkOrder)
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
GridView1.EditIndex = -1
DataBind()
End Using
End Using

Thanks in advance!
SMBrown
 
there are a couple issues. some directly related to the problem, others are just good programming sense.

1. do not put the connection string in the markup! this is a common piece of code that is required. put it in the web.config file and reference the config.

2. do not use datasource controls. you cannot debug them or step through the code. instead write the code yourself.

3. the id is editable. to update the record you need both the original id and the new id. BTW, db id's should not be editable. if you have control over the db design use a surrogate key for user defined "keys" and use a database key for the primary/foreign keys.

4. this is your problem PropertyName="selectedrow.cells(0).text"

it's been awhile since I've messed with webcontrols/forms. there are plenty of examples online about how to extract values from gridviews. goggle: update database with gridview, or something like that.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I think that the problem I am having is that I need to query the DB based on values chosen by the user, and fill the grid with that data, which is not working if I use the AccessDataSource. SO I agree that I need to get rid of it. I want to do everything in the VB code on the backside.

I always put the connection string in this way when I am troubleshooting, and then set it up to reference later. Also ID is going to be read only once I get the ball rolling on this thing.

I need a good example that works, and that someone can help me pick apart. My biggest problems are (1)that I don't understand how to get the new values entered by the user from the test boxes in edit, and (2) how to load those into the update statement to update the DB.

Thanks for taking the time to help.
SMBrown
 
there are plenty of examples on the web. google "gridview database update" or something like that.

another option is not using the inline editing functionality of the gridview. instead redirect the user to an update form. then you don't need to mess with the grid.

once that is working, you could use modal popups (ms ajax and jquery thickbox) to pop up a modal for the user to enter the information. all within "1" webpage.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top