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