By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

datagrid and problems

P: n/a
lu
I have a page with datagrid (for viewing,
delete,edit,update) and text fields with button to add
info to database.
When user comes to page the datagrid selects info from
database, binds in columns, and adds a delete buttoncolumn
and editcommandcolumn. Below that, I have several text
boxes with a button with an onclick event to add new
information to the database table the grid pulls from.

A)Select Info binds fine.
B)Sumbitting values to be added works fine.
C)Delete option works fine.
D)<problem>Edit option tries to runs the delete sub , then
the edit sub.
E)<problem>Update option triggers the
RequiredFieldValidator used when new values are being
added.
F)Code is below for review
**********

<%@ Import NameSpace="System.Data.SqlClient" %>
<%@ Import NameSpace="System.Data"%>
<%@ Page Language="vb" %>
<HTML>
<script runat="Server">
Dim Conn As SqlConnection
Dim strSelect As String 'string based query
Dim Cmd As SqlCommand 'will hold sql commands
Dim ParmInfo as SQLParameter
Dim NID as Integer 'holds nurse computer generated id
Dim DBCommand as SQLDataAdapter

Sub Page_Load
Call BuildGrid()
End Sub

Sub BuildGrid
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CCAN;Trusted_Connection=yes;")
strSelect = "Select PNID, LastName, FirstName, MiddleName,
IDNum, SSNum, Email, Phone1, Phone2 from
TF_Preassigned_Table Order by LastName "

'response.write (strSelect)
Cmd = New SqlCommand(strSelect, Conn)
Try
Conn.Open()
dtrItem.DataSource = cmd.ExecuteReader()
dtrItem.DataBind()
Catch TfHrException As SqlException
TfHrExceptionLabel.Text = TfHrException.Message
Finally
Conn.Close()
End Try
End Sub

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CCAN;Trusted_Connection=yes;")
Cmd = New SQLCommand("TF_sp_insert_preassigned_info", Conn)
Cmd.CommandType = CommandType.StoredProcedure
'Add Return Value Parameter
ParmInfo = Cmd.Parameters.Add("ReturnValue", SqlDbType.Int)
ParmInfo.Direction = ParameterDirection.ReturnValue
'Add UID input parameter
Cmd.Parameters.Add("@ID", IDNum.Text)
Cmd.Parameters.Add("@FirstName", FirstName.Text)
Cmd.Parameters.Add("@LastName", LastName.Text)
Cmd.Parameters.Add("@MiddleName", MiddleName.Text)
Cmd.Parameters.Add("@SSNum", SSNum1.Text)
Cmd.Parameters.Add("@UserEmail", Email.Text)
Cmd.Parameters.Add("@Phone1", Phone1.Text)
Cmd.Parameters.Add("@Phone2", Phone2.Text)
Conn.Open()
Cmd.ExecuteNonQuery()
NID = Cmd.Parameters("ReturnValue").Value
Conn.Close()
Call BuildGrid()
End Sub

Sub Click_Grid(ByVal Sender As Object, ByVal E as
DataGridCommandEventArgs)

Dim TheID as String
TheID = E.Item.Cells(0).Text
response.write (TheID)
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CCAN;Trusted_Connection=yes;")
Cmd = New SQLCommand("TF_sp_delete_preassigned_info", Conn)
Cmd.CommandType = CommandType.StoredProcedure

'Add Return Value Parameter
ParmInfo = Cmd.Parameters.Add("@Progress", SqlDbType.Int)
ParmInfo.Direction = ParameterDirection.Output
'Add UID input parameter

Cmd.Parameters.Add("@PNID", TheID)
'Cmd.CommandText = "Delete from TF_Preassigned_table Where
PNID = "&TheID
Cmd.Connection = Conn
Cmd.Connection.Open
Cmd.ExecuteNonQuery()
NID = Cmd.Parameters("@Progress").Value
Conn.Close
if NID = 0 Then
response.write ("The could not be deleted.")
else
response.write ("Record found")
end if
Call BuildGrid()
End Sub

Sub Edit_Grid(sender As Object, e As
DataGridCommandEventArgs)
dtrItem.EditItemIndex = e.Item.ItemIndex
Call BuildGrid()
End Sub

Sub Update_Grid(ByVal Sender as Object, ByVal E as
DataGridCommandEventArgs)
Dim TheID as String
Dim LastName as String
Dim FirstName as String
Dim ID as String
Dim SSNUM as Integer
Dim Email as String

TheID = E.Item.Cells(0).Text
LastName= CType(e.Item.Cells(1).Controls(0), TextBox).Text
FirstName= CType(e.Item.Cells(2).Controls(0), TextBox).Text
ID = CType(e.Item.Cells(3).Controls(0), TextBox).Text
SSNUM= CType(e.Item.Cells(4).Controls(0), TextBox).Text
Email= CType(e.Item.Cells(5).Controls(0), TextBox).Text
'havent finished this
End Sub
</script>
<body>
<form runat="server" ID="Form1">
<asp:Label ID="TfHrExceptionLabel" runat="server" />
<BR>
<asp:DataGrid ID="dtrItem"
AutoGenerateColumns="false" enableviewstate="false"
runat="server" ItemStyle-BackColor="#DEDFDE"
AlternatingItemStyle-BackColor="LightSteelBlue"
HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="White"
HeaderStyle-BackColor="Black" Font-Names="Verdana" Font-
Size="X-Small" ForeColor="Black" BackColor="White"
Cellpadding="3" GridLines="None" CellSpacing="1"

oneditcommand="Edit_Grid"
onupdatecommand="Update_Grid"
onitemcommand="Click_Grid"

<Columns>
<asp:BoundColumn HeaderText="System ID" DataField="PNID"
readonly="true"/>
<asp:BoundColumn HeaderText="Last name" DataField
="LastName"/>
<asp:BoundColumn HeaderText="First name"
DataField="FirstName"/>
<asp:BoundColumn Headertext="ID" DataField="IDNum"/>
<asp:BoundColumn HeaderText="Social Security"
DataField="SSNum"/>
<asp:BoundColumn HeaderText="Email" DataField="Email"/>
<asp:ButtonColumn HeaderText="Click to delete"
ButtonType="PushButton" Text="Delete record" />
<asp:EditCommandColumn EditText="Edit" UpdateText="Update"
ItemStyle-Wrap="False" HeaderText="Edit" HeaderStyle-
Wrap="False"/>
</Columns>

</asp:DataGrid>
<br>
<table border="1">
<tr>
<td
colspan="2"><asp:Label ID="TableHeading" text="Add new
nurse" Runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<asp:label
id="Label1" Runat="server" text="ID:"></asp:label>
</td>
<td>

<asp:textbox id="IDNum" runat="server"
columns="10"></asp:textbox>

<asp:RequiredFieldValidator ID="id"
ControlToValidate="IDNum" Display="Dynamic"
Runat="server">You must enter an
ID.</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:label
id="Label2" Runat="server" text="First name:"></asp:label>
</td>
<td>

<asp:textbox id="FirstName" runat="server"
columns="10"></asp:textbox>

<asp:RequiredFieldValidator ID="fname"
ControlToValidate="FirstName" Display="Dynamic"
Runat="server">You must enter first
name.</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:label
id="Label3" Runat="server" text="Last name:"></asp:label>
</td>
<td>

<asp:textbox id="LastName" runat="server"
columns="10"></asp:textbox>

<asp:RequiredFieldValidator ID="lname"
ControlToValidate="LastName" Display="Dynamic"
Runat="server">You must enter last
name.</asp:RequiredFieldValidator>
</td>
<tr>
<td>
<asp:label
id="Label4" Runat="server" text="Middle name:"></asp:label>
</td>
<td>

<asp:textbox id="MiddleName" runat="server"
columns="10"></asp:textbox>
</td>
</tr>
<tr>
<td>
<asp:label
id="Label5" Runat="server" text="Social
security:"></asp:label>
</td>
<td>

<asp:textbox id="SSNum1" runat="server"
columns="9" MaxLength="9"></asp:textbox>

<asp:RequiredFieldValidator Runat="server"
ID="ReqSSNum" ControlToValidate="SSNum1"

ErrorMessage="You must provide a social security
number"

display="Dynamic"/>

<asp:RegularExpressionValidator Runat="server"
ID="ReqExpSSNum"

ControlToValidate="NurseSSNum1"
ValidationExpression="^\d{3}\d{2}\d{4}$"

ErrorMessage= "Social security must be in the
format XXXXXXXXX"

display="Dynamic"
/>

</td>
</tr>
<tr>
<td>
<asp:label
id="Label6" Runat="server" text="Email
address:"></asp:label>
</td>
<td>

<asp:textbox id="Email" runat="server"
columns="10"></asp:textbox>
</td>
</tr>
<tr>
<td>
<asp:label
id="Label7" Runat="server" text="Phone 1:"></asp:label>
</td>
<td>

<asp:textbox id="Phone1" runat="server"
columns="10"></asp:textbox>
</td>
</tr>
<tr>
<td>
<asp:label
id="Label8" Runat="server" text="Phone 2:"></asp:label>
</td>
<td>

<asp:textbox id="Phone2" runat="server"
columns="10"></asp:textbox>
</td>
</tr>
</table>
<asp:Button id="Add" Text="Add
Nurse" OnClick="SubmitBtn_Click"
Runat="server"></asp:Button>
<P>
<asp:HyperLink id="hyplnk"
runat="server" Text="Main menu"
navigateurl="../TF"
/>
</form>
</body>
</HTML>

Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.