Edit Command doesn't work

R

Rudy

Hello all,

I have a edit button on my data grid. My code for the edit comnd is ....

Private Sub grdUinv_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdUinv.EditCommand

grdUinv.EditItemIndex = e.Item.ItemIndex


End Sub

But for some reason, when I hit Edit button, it just flashes and nothing
changes. I tried with smart nav on and off. Same thing. I seem to recall
there is something I need to code in the HTML source, but I can't remeber
what. For now, I haven't touched the HTML.
Any help on this would be appreciated!

TIA!!!


Rudy
 
R

Rudy

The plot thickens....
So if I do this to my code..
Sub BindDataGrid()
Dim connUinv As New SqlConnection(strConnString)
Dim cmdSelect As SqlCommand

cmdSelect = New SqlCommand("Select * From IMSProducts", connUinv) '
Order by " & strSortField, connUinv)
connUinv.Open()
grdUinv.DataSource = cmdSelect.ExecuteReader
'COMMENTED THIS LINE OUT, AND NOW EDIT COMMAND WORKS, uncommeted it
doesn't work
grdUinv.DataBind()


connUinv.Close()

End Sub
and comment out grdUinv.databind, the Edit command kinda works. I have to
hit it twice for it to take, and then it does cancel when I hit cancel, which
I didn't set that up, but when I edit say row#6, it highlights the origianl
row I highlited. Then bad things begin to happen, like my panel overlaps the
grid, and everything gets goofy.

I fearI'm refreshing enough, or too much, or something like that. I hate to
put my whole code up, but I think I have a few lines not put up right, and I
think it would be hard to help me without seeing everything is going on.

just a note, my add button works great!, it's the edit button, and the
ability to page that is giving me flack. I'm tryhing this page from scratch,
instead of using a template or example. I have done that., and got ti to
work. Now I'm just going from books that I have and examples of functions.
I appreaciate all who help us newbies, I know I'm always learning from the
little tidbits of knowledge that get posted on this board.

Rudy

<CODE>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then

Dim connUinv As New SqlConnection(strConnString)
Dim cmdSelect As SqlCommand

'Hide panel "pnladd"
pnlAdd.Visible = False


'Create a command object for query, using a SP
cmdSelect = New SqlCommand("UinvSelect", connUinv)
cmdSelect.CommandType = CommandType.StoredProcedure
'Create a DataAdapter
Dim daUinv As New SqlDataAdapter
daUinv.SelectCommand = cmdSelect
'Populate Dataset
Dim dsUinv As New DataSet
daUinv.Fill(dsUinv)
connUinv.Close()

'Specify the datasource call databind
grdUinv.DataSource = dsUinv
grdUinv.DataBind()
End If


End Sub
Protected strConnString As String =
ConfigurationSettings.AppSettings("ConnString")
Public strMsg As String
Public strErrorMsg As String

'Create the connection, made a "strconnection" in web config
Sub BindDataGrid()
Dim connUinv As New SqlConnection(strConnString)
Dim cmdSelect As SqlCommand

cmdSelect = New SqlCommand("Select * From IMSProducts", connUinv) '
Order by " & strSortField, connUinv)
connUinv.Open()
grdUinv.DataSource = cmdSelect.ExecuteReader
'COMMENTED THIS LINE OUT, AND NOW EDIT COMMAND WORKS, uncommeted it
doesn't work
grdUinv.DataBind()


connUinv.Close()

End Sub
'This create a new dataset if needed because cache requires it
Private Function CreateDataSet() As DataSet
Dim strNewDS As String = "SELECT * " & _
"FROM IMSProducts"

Dim connNewDS As New SqlConnection(strConnString)
connNewDS.Open()
Dim cmdSelect As New SqlCommand(strNewDS, connNewDS)
Dim DAUinv As New SqlDataAdapter(cmdSelect)
Dim dsUinv As New DataSet
DAUinv.Fill(dsUinv)

Return dsUinv


End Function
Private Sub GetDataSource()
'If the dat can be found in cache, use it
If Not IsNothing(Cache("dvproducts")) Then
'All items in the cache are of type Object, explicitly cast it
before working with it further
dvProducts = CType(Cache("dvProducts"), DataView)
'if dataview was not found in the cache, create a new dataview
Else : dvProducts = CreateDataSet().Tables(0).DefaultView
'put the new dataview in the cache
Cache("dvProducts") = dvProducts
End If
End Sub
Private Sub BindgrdUinv()
'This line allows the the datagrid to come back after the save
command is thrown
GetDataSource()

With grdUinv
.DataSource = dvProducts
.DataBind()
End With
End Sub
Private Sub grdUinv_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
grdUinv.SortCommand
'This allows you to sort by the columns


End Sub

Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddNew.Click
'Clear any existing values and show the edit form

If pnlAdd.Visible Then
txtProdName.Text = ""
txtModNum.Text = ""
txtSrp.Text = ""
txtCost.Text = ""
txtQty.Text = ""
chkEbay.Checked = False
chkSold.Checked = False
txtComment.Text = ""
Else
pnlAdd.Visible = True
End If

'A command Argument value as a flag for the saveitem method
'This gets reste in the Datagrid_Itemcommnad event handler so that
if the
'user clciks any item in the Datagrid the app ceases to be in "Add"
mode

btnSave.CommandArgument = "Add"
End Sub
Private dvProducts As DataView

Private Sub SaveItem()
Dim strSave As String

'' If btnSave.CommandArgument = "add" Then
strSave = _
"INSERT INTO IMSProducts " & _
"(ProductName, ModelNumber, UnitSRP, UnitCost, UnitsInStock, Ebay,
Sold, Comments)" & _
"Values " & _
"(@ProductName, @ModelNumber, @UnitSRP, @UnitCost, @InStock, @Ebay,
@Sold, @Comments)"

'' End If
Dim connUinv As New SqlConnection(strConnString)
Dim cmdSave As New SqlCommand(strSave, connUinv)

'Add al the required SQL parameters
With cmdSave.Parameters
'The product ID parameter is only needed for updating

If btnSave.CommandArgument <> "Add" Then

.Add(New SqlParameter("@ProductID", SqlDbType.Int)).Value = _
CInt(grdUinv.DataKeys(grdUinv.SelectedIndex).ToString =
txbProductID.Text)
End If
'the rest of the text fields in panel
.Add(New SqlParameter("@ProductName", _
SqlDbType.NVarChar, 40)).Value = txtProdName.Text
.Add(New SqlParameter("@ModelNumber", _
SqlDbType.NVarChar, 50)).Value = txtModNum.Text
.Add(New SqlParameter("@UnitSRP", _
SqlDbType.Money, 8)).Value = CDbl(txtSrp.Text)
.Add(New SqlParameter("@UnitCost", _
SqlDbType.Money, 8)).Value = CDbl(txtCost.Text)
.Add(New SqlParameter("@InStock", _
SqlDbType.Int, 4)).Value = CInt(txtQty.Text)
.Add(New SqlParameter("@ebay", _
SqlDbType.Bit)).Value = chkEbay.Checked
.Add(New SqlParameter("@Sold", _
SqlDbType.Bit)).Value = chkSold.Checked
.Add(New SqlParameter("@Comments", _
SqlDbType.NVarChar, 200)).Value = txtComment.Text

End With

Try
connUinv.Open()
cmdSave.ExecuteNonQuery()
Cache.Remove("dvProducts")
BindgrdUinv()

lblMsg.Text = ("Your a star!! Product successfully saved!.")
pnlAdd.Visible = True

Catch exp As Exception
lblMsg.Text = "Databse error! Product not saved to the " & _
"database. Error mesaage: " & exp.Message
Finally
connUinv.Close()

End Try
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
If IsValid Then
SaveItem()
txtProdName.Text = ""
txtModNum.Text = ""
txtSrp.Text = ""
txtCost.Text = ""
txtQty.Text = ""
chkEbay.Checked = False
chkSold.Checked = False
txtComment.Text = ""
End If

End Sub

Private Sub grdUinv_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdUinv.ItemCommand

If e.Item.ItemType = ListItemType.Pager Or _
e.Item.ItemType = ListItemType.Header Then Exit Sub
grdUinv.EditItemIndex = -1

'' Dim btn As Button = CType(e.CommandSource, Button)
'' If btn.Text = "Edit" Then
'' txbProductID.Text = e.Item.Cells(1).Text
'' txtProdName.Text = e.Item.Cells(2).Text
'' txtModNum.Text = e.Item.Cells(3).Text
'' txtSrp.Text = Microsoft.VisualBasic.Right(e.Item.Cells(4).Text, _
'' Len(e.Item.Cells(4).Text) - 1)
'' txtCost.Text =
Microsoft.VisualBasic.Right(e.Item.Cells(5).Text, _
'' Len(e.Item.Cells(5).Text) - 1)
'' txtQty.Text = e.Item.Cells(6).Text
'' chkEbay.Checked = _
'' CType(e.Item.Cells(7).FindControl("chkEbay"), _
'' CheckBox).Checked
'' chkSold.Checked = _
'' CType(e.Item.Cells(8).FindControl("chkSold"), _
'' CheckBox).Checked
'' pnlAdd.Visible = True
'' Else



'' End If
End Sub


Private Sub grdUinv_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdUinv.EditCommand

grdUinv.EditItemIndex = e.Item.ItemIndex
BindDataGrid()


End Sub


Private Sub grdUinv_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles grdUinv.SelectedIndexChanged

End Sub
End Class
 
R

Rudy

It'a always something easy, turns out that "Allow Custom Paging" was false, I
turned it on, my edit command works fine

Rudy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top