2014年7月13日 星期日

[RESOLVED] Get Values from Gridview


I'm almost there, I'm sure it's something simple.  I have a gridview where staff are to select 10 coworkers (via checkbox) to evaluate them.  I want to send the emails of the 10 selected staff to the database.  The code below sends the first 10 in the list,
not the selected 10, to the database.  What changes do I need to make to grab the 10 selected emails.  Thanks in advance!


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim rowIndex As Integer = 0
Dim myConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("cs360").ConnectionString)
Dim myCommand As New SqlCommand("uspEmail_Add", myConnection)

myCommand.CommandType = CommandType.StoredProcedure

For i As Integer = 0 To gvStaff.Rows.Count - 1
myCommand.Parameters.Clear()

If DirectCast(gvStaff.Rows(i).Cells(0).FindControl("chkSel"), CheckBox).Checked = True Then

Dim ctlOMIT As CheckBox = DirectCast(gvStaff.Rows(rowIndex).Cells(0).FindControl("chkSel"), CheckBox)
Dim ctlUID As Label = DirectCast(gvStaff.Rows(rowIndex).Cells(0).FindControl("lbluID"), Label)
Dim ctlUEmail As Label = DirectCast(gvStaff.Rows(rowIndex).Cells(0).FindControl("lblEmail"), Label)

Dim uID As New SqlParameter("@uID", SqlDbType.Int)
uID.Value = CInt(ctlUID.Text)
myCommand.Parameters.Add(uID)

Dim uEmail As New SqlParameter("@email", SqlDbType.NVarChar)
uEmail.Value = ctlUEmail.Text
myCommand.Parameters.Add(uEmail)

Try
myConnection.Open()
myCommand.ExecuteNonQuery()
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert Error: "
msg += ex.Message
Throw New Exception(msg)
Finally
myConnection.Close()
End Try

rowIndex += 1

End If
Next

End Sub







try changing code lines where u r reading gridview values... like this


Dim ctlOMIT As CheckBox = DirectCast(gvStaff.Rows(i).Cells(0).FindControl("chkSel"), CheckBox)
Dim ctlUID As Label = DirectCast(gvStaff.Rows(i).Cells(0).FindControl("lbluID"), Label)
Dim ctlUEmail As Label = DirectCast(gvStaff.Rows(i).Cells(0).FindControl("lblEmail"), Label)

hope this helps...



GENIUS KEDAR!  THANKS!


沒有留言:

張貼留言