Now i am trying to create an edit field... i want to be able to alter the sub cat value and also select the main cat froma ddl (dont know if that is possible in an edit field)....
my code is not working, is there an easy way of doing this.,,,
Sub Page_Load()
'This checks to see if their is a cookie on the viewers system.
If Request.Cookies("AdminNameCookie") is nothing Then
'If there is not one then it sends the person back to the login page.
Response.Redirect("default.aspx")
End If
If Not Page.IsPostBack then
'This binds the data to the data grids.
dgCategories.DataSource() = getCategories()
dgCategories.DataBind()
dgSubCategories.DataSource() = getSubCategories()
dgSubCategories.DataBind()
'This binds the categories data to the drop down list to create sub categories
ddlSelectCategories.DataSource = getCategories()
ddlSelectCategories.DataValueField = "CategoryID"
ddlSelectCategories.DataTextField = "CategoryName"
ddlSelectCategories.DataBind()
End if
End Sub
'This is the event for the category creation.
Sub btnCreateCategory_Click(sender As Object, e As EventArgs)
'This declares the variables that will connect to the database.
Dim ConnectionString as string = ConfigurationSettings.AppSettings("ConnectionStrin g")
Dim dbConnection As New OleDbConnection(ConnectionString)
dbConnection.Open()
Dim commandString as String = "INSERT INTO tblCategories (categoryName) Values(@categoryName)"
Dim dbCommand as new OleDbCommand(commandString, dbConnection)
dbCommand.Parameters.Add("@categoryName", OleDbType.VarChar, 10).Value = txtCategoryName.Text
dbCommand.ExecuteNonQuery()
dbConnection.Close()
'this displays a label to show the category has been created.
lblCreateCategory.Visible = "True"
txtCategoryName.text = ""
End Sub
'This is the event for the sub category creation.
Sub btnCreateSubCategory_Click(sender As Object, e As EventArgs)
'This declares the variables that will connect to the database.
Dim ConnectionString as string = ConfigurationSettings.AppSettings("ConnectionStrin g")
Dim dbConnection As New OleDbConnection(ConnectionString)
dbConnection.Open()
Dim commandString as String = "INSERT INTO tblSubCategory (SubCategoryName, CategoryID, CategoryName) Values(@SubCategoryName, @CategoryID, @CategoryName)"
Dim dbCommand as new OleDbCommand(commandString, dbConnection)
dbCommand.Parameters.Add("@SubCategoryName", OleDbType.VarChar, 50).Value = txtSubCategoryName.Text
dbCommand.Parameters.Add("@CategoryID", OleDbType.VarChar, 10).Value = ddlSelectCategories.SelectedItem.Value
dbCommand.Parameters.Add("@CategoryName", OleDbType.VarChar, 50).Value = ddlSelectCategories.SelectedItem.Text
dbCommand.ExecuteNonQuery()
dbConnection.Close()
'this displays a label to show it created the sub category
lblCreateSubCategory.Visible = "True"
txtSubCategoryName.text = ""
End Sub
'This sub is to select a row to be edited.
Sub dgSubCategories_Edit(Sender as Object, E as DataGridCommandEventArgs)
dgSubCategories.EditItemIndex = E.Item.ItemIndex
End Sub
'This resets the edited row when the cancel button is selected.
Sub dgSubCategories_Cancel(Sender as Object, E as DataGridCommandEventArgs)
dgSubCategories.EditItemIndex = -1
End Sub
Sub dgSubCategories_Update(sender as object, E as DataGridCommandEventArgs)
'Read the values of the updated row.
Dim iSubCatID as Integer = E.Item.Cells(1).Text
Dim strSubCat as string = CType(E.Item.Cells(2).Controls(0), TextBox).Text
'Construct the sql statement
Dim strSQL as String = _
"UPDATE [tblSubCategories] SET [SubCategoryName] = @SubCategoryName WHERE [SubCategoryID] = @SubCategoryID"
Const strConnString as String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwroot\application\admin\db1.md b"
Dim objConn as New OleDbConnection(strConnString)
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
'Add Parameters here
Dim parameterSubCategoryName as OleDbParameter = _
new OleDbParameter("@SubCategoryName", OleDbType.VarWChar, 75) parameterSubCategoryName.Value = strSubCat
myCommand.ExecuteNonQuery()
objConn.Close
dgSubCategories.EditItemIndex = -1
End Sub
'The following two functions get all the details from the database to be displayed on the page!
Function getCategories() As System.Data.DataSet
Dim ConnectionString As String = ConfigurationSettings.AppSettings("ConnectionStrin g")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )
Dim queryString As String = "SELECT [tblCategories].* FROM [tblCategories]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Function getSubCategories() As System.Data.IDataReader
Dim connectionString As String = ConfigurationSettings.AppSettings("ConnectionStrin g")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )
Dim queryString As String = "SELECT [tblSubCategory].* FROM [tblSubCategory]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
dbConnection.Open
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)
Return dataReader
End Function
Sub dgSubCategories_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
'
<p>
<asp:DataGrid id="dgCategories" runat="server" AutoGenerateColumns="False" CellPadding="3">
<Columns>
<asp:BoundColumn DataField="CategoryID" HeaderText="Cat ID" ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn DataField="CategoryName" HeaderText="Category Name" ItemStyle-HorizontalAlign="Center" />
</Columns>
</asp:DataGrid>
</p>
<p>
<asp:DataGrid id="dgSubCategories" runat="server" AutoGenerateColumns="False" CellPadding="3" OnEditCommand="dgSubCategories_Edit" OnSelectedIndexChanged="dgSubCategories_SelectedIn dexChanged" OnCancelCommand="dgSubCategories_Cancel" OnUpdateCommand="dgSubCategories_Update">
<Columns>
<asp:EditCommandColumn EditText="Edit" ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />
<asp:BoundColumn DataField="SubCategoryID" HeaderText="Sub Cat ID" ItemStyle-HorizontalAlign="Center" ReadOnly="True" />
<asp:BoundColumn DataField="SubCategoryName" HeaderText="Sub Category Name" ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn DataField="CategoryName" HeaderText="Main Category" ItemStyle-HorizontalAlign="Center" />
</Columns>
</asp:DataGrid>
</p>
thanks for any help...
David Jenkins
-------------------------------------------------------------
Do you want to make extra money around your commitments?
Credit cards, bills, loans and a mortgage - all getting you down?
Is your pension going to be enough when you retire?
There is a solution visit
http://www.1stmillion.co.uk
or call 01772 489521