473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Updating an Access Yes/No field from a datagrid

I have a DataGrid that is configured to use the Edit/Update/Cancel concept
correctly. My grid shows values from 5 database fields. I only need to
update that last 4 fields. The last field is a Yes/No value in Access.
Using the OleDbCommand, if I do not consider the Yes/No field, the
ExecuteNonQuery command, using my UPDATE SQL statement, updates the record
correctly. However, if I put the Yes/No field into the mix, ExecuteNonQuery
returns 0 (0 records updated). The Yes/No field in Access is setup to show
checkboxes in the database itself. I have tried setting the value to update
to on"/"off", "yes"/"no", -1/0, and none of these seem to work.

In the mean time, I have recreated the Yes/No field to be a Text Field which
stores "Yes" or "No", and the checkboxes update correctly in both display
mode and when I go into Edit mode. However, nothing gets updated now (where
the other 4 fields were OK earlier). I am posting my code below if anyone
could take a look, or have any ideas.

Thanks for any help.
Harry

Code:
<%@ Page Language="VB" Debug="True"%>
<%@ Register TagPrefix="mytree" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls"%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.String" %>
<%@ import Namespace="System.Web.Mail" %>

<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindData()
End If
End Sub

Public Sub BindData()
Dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
objConn.Open()
Dim oaUser As OleDbDataAdapter
Dim UserDS as DataSet = New DataSet()
oaUser = New OleDbDataAdapter("Select * FROM tblPlayerStats ORDER BY
Name ASC", objConn)

oaUser.Fill(UserDS,"tblPlayerStats")
objConn.Close

UserGrid.DataSource = UserDS.Tables("tblPlayerStats")
UserGrid.DataBind()
End Sub

Public Sub UserGrid_Edit (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = E.Item.ItemIndex
BindData()
End Sub

Public Sub UserGrid_Cancel (Source As Object, E As
DataGridCommandEventArgs)
UserGrid.EditItemIndex = -1
BindData()
End Sub

Public Sub UserGrid_Update (Source As Object, E As
DataGridCommandEventArgs)
Dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
' Dim cmd As OleDbCommand = new OleDbCommand("UPDATE tblPlayerStats
SET EMail = @EMail, RealName = @RealName, Phone = @Phone, Paid = @Paid WHERE
Name = @Name", objConn)
Dim cmd As OleDbCommand = new OleDbCommand("UPDATE tblPlayerStats SET
Paid = @Paid WHERE Name = @Name", objConn)

Dim chkPaid As CheckBox =
CType(e.Item.FindControl("edit_chkDonated"), CheckBox)
Dim sName As String = e.Item.Cells(2).Text
Dim sRealName As String = e.Item.Cells(3).Text
Dim sEmail As String = e.Item.Cells(4).Text
Dim sPhone As String = e.Item.Cells(5).Text
Dim iRet As Integer
Dim sPaid As String

if chkPaid.Checked then
sPaid = "Yes"
else
sPaid = "No"
end if
'labelPaid.Text = "sPaid: " & sPaid

cmd.Parameters.Add(new OleDbParameter("@Name", sName))
' cmd.Parameters.Add(new OleDbParameter("@EMail", sEmail))
' cmd.Parameters.Add(new OleDbParameter("@RealName", sRealName))
' cmd.Parameters.Add(new OleDbParameter("@Phone", sPhone))
cmd.Parameters.Add(new OleDbParameter("@Paid", "No"))

objConn.Open()
iRet = cmd.ExecuteNonQuery()
objConn.Close
labelEmail.Text = "Rows updated: " & iRet
' labelEmail.Text = "RealName: " & sRealName & " Email: " & sEmail & "
Phone: " & sPhone & " Name: " & sName & " Paid: " & sPaid

' UserGrid.EditItemIndex = -1
' BindData()

End Sub

Public Sub UserGrid_Command(sender As Object, e As
DataGridCommandEventArgs)
Select (CType(e.CommandSource, LinkButton)).CommandName

Case "Delete"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("DELETE FROM
tblPlayerStats WHERE Name = @Name", objConn)

cmd.Parameters.Add(new OleDbParameter("@Name",
e.Item.Cells(2).Text))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
Case "ViewPWD"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
Dim sql As String = "SELECT Password FROM tblPlayerStats WHERE
Name = '" & e.Item.Cells(2).Text & "'"
objConn.Open()
Dim cmd as New OleDbCommand(sql, objConn)
Dim reader as OleDbDataReader = cmd.ExecuteReader

reader.Read()
ViewPasswordLabel.Text = "Name: " & e.Item.Cells(2).Text & "
Password: " & reader.Item("Password")
objConn.Close()

Case "ResetPWD"
Dim objConn as new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("UPDATE
tblPlayerStats SET Password = @Password WHERE Name = @Name", objConn)

cmd.Parameters.Add(new OleDbParameter("@Name",
e.Item.Cells(2).Text))
cmd.Parameters.Add(new OleDbParameter("@Password", "faatc1"))
objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close
Case Else
' Do Nothing

End Select

BindData()
End Sub

Public Sub UserGrid_ItemCreated(sender As Object, e As
DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem

' Add confirmation to Delete button
Dim tblCell As TableCell
Dim btnDelete As LinkButton

tblCell = e.Item.Cells(1)
btnDelete = tblCell.Controls(0)
btnDelete.Attributes.Add("onclick", "return confirm('Are you
sure you want to delete this player?');")
End Select
End Sub

Public Sub AddUser_Click(sender As Object, e As EventArgs)
PlayerNameLabel.Visible = "True"
AddPlayerName.Visible = "True"
RealNameLabel.Visible = "True"
AddRealName.Visible = "True"
EmailLabel.Visible = "True"
AddEmail.Visible = "True"
PhoneLabel.Visible = "True"
AddPhone.Visible = "True"
AddPaid.Visible = "True"
AddNewUser.Visible = "True"
AddCancel.Visible = "True"
End Sub

Public Sub AddNewUser_Click(sender As Object, e As EventArgs)
Dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\inetpub\wwwroot\applecart\pool03.mdb")
Dim cmd As OleDbCommand = new OleDbCommand("INSERT INTO tblPlayerStats
(Name,Email,Password,RealName,Phone,Paid)
values(@Name,@Email,@Password,@RealName,@Phone,@Pa id)", objConn)
Dim sPaid As String

if AddPaid.Checked then
sPaid = "Yes"
else
sPaid = "No"
end if

cmd.Parameters.Add(new OleDbParameter("@Name", AddPlayerName.Text))
cmd.Parameters.Add(new OleDbParameter("@Email", AddEmail.Text))
cmd.Parameters.Add(new OleDbParameter("@Password", "faatc1"))
cmd.Parameters.Add(new OleDbParameter("@RealName", AddRealName.Text))
cmd.Parameters.Add(new OleDbParameter("@Phone", AddPhone.Text))
cmd.Parameters.Add(new OleDbParameter("@Paid", sPaid))

objConn.Open()
cmd.ExecuteNonQuery()
objConn.Close

PlayerNameLabel.Visible = "False"
AddPlayerName.Visible = "False"
RealNameLabel.Visible = "False"
AddRealName.Visible = "False"
EmailLabel.Visible = "False"
AddEmail.Visible = "False"
PhoneLabel.Visible = "False"
AddPhone.Visible = "False"
AddPaid.Visible = "False"
AddNewUser.Visible = "False"
AddCancel.Visible = "False"

BindData()
End Sub

Public Sub AddCancel_Click(sender As Object, e As EventArgs)
PlayerNameLabel.Visible = "False"
AddPlayerName.Visible = "False"
RealNameLabel.Visible = "False"
AddRealName.Visible = "False"
EmailLabel.Visible = "False"
AddEmail.Visible = "False"
PhoneLabel.Visible = "False"
AddPhone.Visible = "False"
AddPaid.Visible = "False"
AddNewUser.Visible = "False"
AddCancel.Visible = "False"

' Reset text fields (for next time)
AddPlayerName.Text = ""
AddRealName.Text = ""
AddEmail.Text = ""
AddPhone.Text = ""
AddPaid.Checked = "False"
End Sub

</script>

<html>
<head>
</head>
<body>
<form method="post" runat="server">
<asp:label id="labelPaid" runat="server"/>
<asp:label id="labelEmail" runat="server"/>
<asp:button id="AddUser_Button" Text="Add New User" runat="server"
onClick="AddUser_Click" />
<asp:label id="ViewPasswordLabel" runat="server"/>
<table border=0>
<tr><td>
<asp:datagrid id="UserGrid" runat=server AutoGenerateColumns="False"
BorderStyle="Dotted" BorderWidth="2"
BackgroundColor="red"
CellPadding="5"
Font-Name="Arial" Font-Size="8pt"
OnEditCommand="UserGrid_Edit"
OnCancelCommand="UserGrid_Cancel"
OnUpdateCommand="UserGrid_Update"
OnItemCommand="UserGrid_Command"
OnItemCreated="UserGrid_ItemCreated">

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<EditItemStyle BackColor="yellow">
</EditItemStyle>

<ItemStyle Wrap="false">
</ItemStyle>

<Columns>
<asp:EditCommandColumn
ButtonType ="LinkButton"
CancelText = "Cancel"
EditText = "Edit"
UpdateText = "Update">
</asp:EditCommandColumn>
<asp:ButtonColumn
HeaderText="Delete?"
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>
<asp:BoundColumn
DataField = "Name"
HeaderText = "Name"
ReadOnly = "True" />
<asp:BoundColumn
DataField = "RealName"
HeaderText = "Real Name"/>
<asp:BoundColumn
DataField = "EMail"
HeaderText = "EMail"/>
<asp:BoundColumn
DataField = "Phone"
HeaderText = "Phone"/>
<asp:TemplateColumn HeaderText="Donated?">
<ItemTemplate>
<asp:Checkbox runat="server" name="chkDonated" ID="chkDonated"
enabled="False" Checked='<%# IIF(DataBinder.Eval(Container.DataItem, "Paid")
="Yes", "True", "False") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Checkbox runat="server" id="edit_chkDonated" enabled="True"
checked='<%# IIF(DataBinder.Eval(Container.DataItem, "Paid") = "Yes",
"True", "False") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn
HeaderText="View PWD"
ButtonType="LinkButton"
Text="View PWD"
CommandName="ViewPWD"/>
<asp:ButtonColumn
HeaderText="Reset PWD"
ButtonType="LinkButton"
Text="Reset"
CommandName="ResetPWD"/>
</Columns>
</asp:datagrid>
</td><td valign="top">
<table border=0 bgcolor=yellow>
<tr><td><asp:label id="PlayerNameLabel" Text="Player Name:"
visible="false" runat="server" /></td>
<tr><td><asp:textbox id="AddPlayerName" runat="server"
visible="false" /></td>
<tr><td><asp:label id="RealNameLabel" Text="Real Name:"
visible="false" runat="server" /></td>
<tr><td><asp:textbox id="AddRealName" runat="server" visible="false"
/></td>
<tr><td><asp:label id="EmailLabel" Text="EMail:" visible="false"
runat="server" /></td>
<tr><td><asp:textbox id="AddEmail" runat="server" visible="false"
/></td>
<tr><td><asp:label id="PhoneLabel" Text="Phone:" visible="false"
runat="server" /></td>
<tr><td><asp:textbox id="AddPhone" runat="server" visible="false"
/></td>
<tr><td><asp:checkbox id="AddPaid" Text="Paid?" runat="server"
visible="false" /></td>
<tr><td><asp:button id ="AddNewUser" Text="Add This User"
runat="server" visible="false" onClick="AddNewUser_Click" />
<td><asp:button id ="AddCancel" Text="Cancel" runat="server"
visible="false" onClick="AddCancel_Click"/>
</table>
</td></table>
</form>
</body>
</html>

Nov 18 '05 #1
1 3766
Jos
Harry Devine wrote:
I have a DataGrid that is configured to use the Edit/Update/Cancel
concept correctly. My grid shows values from 5 database fields. I
only need to update that last 4 fields. The last field is a Yes/No
value in Access. Using the OleDbCommand, if I do not consider the
Yes/No field, the ExecuteNonQuery command, using my UPDATE SQL
statement, updates the record correctly. However, if I put the
Yes/No field into the mix, ExecuteNonQuery returns 0 (0 records
updated). The Yes/No field in Access is setup to show checkboxes in
the database itself. I have tried setting the value to update to
on"/"off", "yes"/"no", -1/0, and none of these seem to work.

In the mean time, I have recreated the Yes/No field to be a Text
Field which stores "Yes" or "No", and the checkboxes update correctly
in both display mode and when I go into Edit mode. However, nothing
gets updated now (where the other 4 fields were OK earlier). I am
posting my code below if anyone could take a look, or have any ideas.


I always use True and False (without quotes)

--

Jos
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dave | last post by:
Please help! I have spent all weekend trying to solve this and its driving me mad! I have a form with a datagrid on. When I click button btnNew, the new row appears in the datagrid but it does...
1
by: Bryan Masephol | last post by:
Hi All I have a OleDbConnection as the "connection" below. I'm retriving a dataset from an access 2002 db and displaying it in a DataGrid. I'm making the connection to my access db file with...
6
by: mkobus | last post by:
Im relatively new, so please be patient with me... I need to update a parent .aspx screen from a popup and close the popup. Normally I would use...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
4
by: A P | last post by:
Hi! I'm a new user of VB.NET 2003 and still learning its feature, one of them is datagrid. I am using Access 2002 Database on my project and trying to update values of a table using datagrid...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
13
by: mfreeman | last post by:
The minimal code (VB.NET 2003) needed to show this problem is shown below. All I do is loop through the records in the table and update them without making any changes. Out of 600 records, about...
0
by: Chet | last post by:
I have a Datagrid that is bound to a Datatable at runtime. I allow the user to select a number of rows using the mouse and then click a button that says "check selected rows", which then cycles...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.