469,943 Members | 2,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,943 developers. It's quick & easy.

add more than one value onclick

LU
I have a datagrid for viewing and deleting. Under that I
have a drop down with binded info and a button to add the
select drop down item into database.
1User can view datagrid and delete info.
2User can select from drop down, click add button and add
selection to database.
A)Datagrid binds and deletes fine.
B)If I select the first value of drop down addition to
database works fine.
C)<problem>if I select the 2nd value of drop down it adds
2 records into the database when it should only add 1. It
adds 1 record for 1st value of drop down and 1 record for
next value selected.
Code below for review.
*************
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page CodeBehind="ViewItemsSub.aspx.vb" Language="vb"
AutoEventWireup="false" Inherits="TF.ViewItemsSub" %>
<HTML>
<script language="vb" runat="server">

Dim intItem As Integer
Dim strSelect As String
Dim Conn As SqlConnection
Dim Cmd As SqlCommand
Dim DBCommand as SQLDataAdapter
Dim DSPageData as New DataSet
Dim ParmInfo as SQLParameter
Dim NID as Integer '
Dim TheID as String

Private Sub Page_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load

intItem = Int32.Parse(Request.QueryString("id"))
Call GetAssignedStaff()
If Not IsPostBack Then
Call BuildDropDown()
end If
End Sub

Sub GetAssignedStaff

'Get assigned staff info
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CA;Trusted_Connection=yes;")
strSelect = "Select A.PNID, B.URPNAID, A.IDNum,
A.FirstName, A.LastName, A.MiddleName"
strSelect = strSelect + " from TF_Preassigned_Table A,
TF_Unit_Request_Preassigned_Assignments B"
strSelect = strSelect + " Where (A.PNID In (Select PNID
from TF_Unit_Request_Preassigned_Assignments Where "
strSelect = strSelect + " URID = " &intItem & "))"
response.write ("<P>")
response.write (strSelect)
Cmd = New SqlCommand(strSelect, Conn)

Try
Conn.Open()

Datagrid1.DataSource = cmd.ExecuteReader()
Datagrid1.DataBind()
Catch TfHrException As SqlException
TfHrExceptionLabel.Text = TfHrException.Message
Finally
Conn.Close()
End Try
'end assigned staff info
End Sub
'preassigned staff options

Sub Click_Grid(ByVal Sender As Object, ByVal E as
DataGridCommandEventArgs)
response.write ("hello")

TheID = E.Item.Cells(0).Text
strSelect ="Delete from
TF_Unit_Request_Preassigned_Assignments Where URPNAID
= "&TheID
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CA;Trusted_Connection=yes;")

Cmd.CommandText = (strSelect)
response.write ("<P>")
response.write(strSelect)
Cmd.Connection = Conn
Cmd.Connection.Open
Cmd.ExecuteNonQuery()
Conn.Close
Call GetAssignedStaff()
End Sub

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

Sub Update_Grid(ByVal Sender as Object, ByVal E as
DataGridCommandEventArgs)

End Sub
'end assigned staff options.

Sub BuildDropDown()

Conn = New SqlConnection("Server=localhost;Initial
Catalog=CA;Trusted_Connection=yes;")

'strSelect = "Select * from TF_Preassigned_Table"
DBCommand = New SQLDataAdapter("Select PNID, LastName,
FirstName,IDNum from TF_Preassigned_Table Order by
LastName", Conn)
'response.write (strSelect)
DBCommand.Fill(DSPageData, "s_Table")
Dim DynColumn As New DataColumn()
With DynColumn
..ColumnName = "FullName"
..DataType = System.Type.GetType("System.String")
..Expression = "LastName+ ' ' +FirstName+ ' ' + IDNum"
End With
DSPageData.Tables("s_Table").Columns.Add(DynColumn )
Assigned.DataTextField = "FullName"
Assigned.DataValueField = "PNID"
Assigned.DataSource = DSPageData.Tables
("s_Table").DefaultView
Assigned.DataBind()
Assigned.Items.Insert(0, "Select one")
Conn.Close()
End Sub

Sub Add_Click(send as object, e as system.eventargs)

'intItem = Int32.Parse(Request.QueryString("id"))
'response.write (intItem)
'response.write ("<BR>")

response.write (Assigned.SelectedItem.Value)
if Assigned.SelectedIndex = 0 then
response.write("Must select a nurse")
else
Conn = New SqlConnection("Server=localhost;Initial
Catalog=CA;Trusted_Connection=yes;")
Cmd = New SQLCommand
("TF_sp_insert_preassigned_assignment", 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("@URID", intItem)
Cmd.Parameters.Add("@PNID", Assigned.SelectedItem.Value)
Cmd.Parameters.Add("@CreatedDTM", datetime.now)
Cmd.Parameters.Add("@CreatedBy", "Admin")
Cmd.Parameters.Add("@ModifiedDTM", DateTime.Now)
Cmd.Parameters.Add("@ModifiedBy", "Admin")
Conn.Open()
Cmd.ExecuteNonQuery()
NID = Cmd.Parameters("ReturnValue").Value
Conn.Close()
response.write("<P>")
response.write(" added")
Call GetAssignedStaff()
'Call BuildDropDown()
end if
End Sub

</script>
<body>
<form runat="server">
<asp:Label ID="TfHrExceptionLabel"
runat="server" />
<asp:Label ID="Label1"
runat="server" />
<BR>
<asp:DataGrid ID="Datagrid1"
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="AssignmentID" DataField="URPNAID" />
<asp:BoundColumn
Headertext="ID" DataField="IDNum" />
<asp:BoundColumn
HeaderText="First name" DataField="FirstName" />
<asp:BoundColumn
HeaderText="Last name" DataField="LastName" />
<asp:ButtonColumn
HeaderText="Click to delete" ButtonType="PushButton"
Text="Delete record" />

</Columns>
</asp:DataGrid>

<asp:DropDownList
id="Assigned"
runat="server"/>
<asp:Button id="Add"
Text="Add "
OnClick="Add_Click"
Runat="server"></asp:Button>
</form>
<P></P>
</body>
</HTML>

Jul 21 '05 #1
0 3430

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eric Chang | last post: by
1 post views Thread by MickG | last post: by
3 posts views Thread by Robin Day | last post: by
5 posts views Thread by Novice Computer User | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.