Too Many Arguments Error with Stored Procedure - asp.net

I have the below code which is triggering a stored procedure to delete a record by matching the parameter with the primary key in the table. I ran the code through debug ang everything is assigning properly, but I keep receiving an error stating there are too many arguments specified. I think it has something to do with my stored procedure because the values are being assigned properly.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim CurrentCommand As String = e.CommandName
Dim CurrentRow As Integer = Int32.Parse(e.CommandArgument.ToString())
Dim ID As String = GridView1.DataKeys(CurrentRow).Value
sqlds.DeleteParameters.Add("KeyOpType", ID)
sqlds.Delete()
End Sub
Stored Procedure in SQL Server 2008
ALTER PROCEDURE [dbo].[GetGenInfo_DeleteMines]
(#KeyOpType int)
AS
Delete FROM GenInfo_OpType
Where KeyOpType = #KeyOpType

Where are you setting your delete command? Do we know for sure that it is calling the correct procedure? I think this is what you're trying to do . Check this:
SQLDataSource with GridView and CRUD

Not sure if this will fix the problem what your having.... but
It appears you are missing the BEGIN in the stored procedure. I'm new to this, but i think that is required.
ALTER PROCEDURE [dbo].[GetGenInfo_DeleteMines]
(#KeyOpType int)
AS
BEGIN <-----------------------------------MISSING THIS
Delete FROM GenInfo_OpType
Where KeyOpType = #KeyOpType
Plus, i don't see where you are calling the SP in the vb code.
EDIT: To give further examples....
My code behind on the gv Row Command would look like this... (if its coming from a button/link on the gv have to add CommandName="SomeCommandName" to the control on the gv)
Protected Sub gvName_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvName.RowCommand
Dim tGrid As GridView = DirectCast(sender, GridView)
Dim tIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = Nothing
Select Case e.CommandName
Case "SomeCommandName"
CallToDelete(tGrid.DataKeys(tIndex).Value)
End Select
End Sub

Related

When I try add insert a value into a asp.net Detailsview I get a "Object reference not set to an instance of an object"

What do I do to fix an "Object reference not set to an instance of an object" error. Which object is it referring to?
code:
Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
Dim dv As DetailsView = New DetailsView
If DetailsViewMode.Insert Then
DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
End If
End Sub
Get Year returns to current year, it appears in the detailsview textbox "PlantYear". I try to insert the value using the above code.
thanks for your help.
Most likely FindControl is not actually finding the control. It would be wise to put a check in to ensure it has actually found what you intended it to find:
Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
Dim dv As DetailsView = New DetailsView
If DetailsViewMode.Insert Then
Dim ctl = dv.FindControl("PlantYear")
If ctl IsNot Nothing Then
DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
Else
Throw New Exception("Control was not found")
End If
End If
End Sub

Unable to cast object of type 'System.Web.UI.WebControls.RadioButtonList' to type 'System.Web.UI.WebControls.TableRow'

I have a 4 tier architecture using asp.net 4.0
1- Web application
2- Business object
3- Business Logic
4- Data Logic
i have used factory method to create dynamic forms. on a radio button list selected index changed event , i want to hide/show a table row which is also created dynamically. table row is available in session. i am using the following code
Private Sub parameter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim rdbtn As RadioButtonList = DirectCast(sender, RadioButtonList)
Dim selectedText As String = rdbtn.SelectedItem.Text
Dim worksRequiredRowId = SessionManager.getWorksRequiredRowId()
Dim worksRequiredRow As TableRow = CType(sender.FindControl(worksRequiredRowId), TableRow)
If selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
worksRequiredRow.Style.Add("display", "table-row")
Else
worksRequiredRow.Style.Add("display", "none")
End If
Catch ex As Exception
End Try
End Sub
And i get the following error.
Unable to cast object of type
'System.Web.UI.WebControls.RadioButtonList' to type
'System.Web.UI.WebControls.TableRow'.
Please help me to find out the solution.
Best ragards.
You can actually hide that particular item from radio button list instead of converting it into table row. Something like this.
If rdbtn.SelectedItem.Text <> selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
rdbtn.Items(itemIndex).Attributes.CssStyle.Add("display", "none")
End If
I hope this helps.
I think your mistake is confusing the generated HTML markup with the raw ASP.NET markup. RadioButtonList does create an HTML for each item, but you can't access that rows at the server-side (to make them visible or hidden).
You should either use the server-side method mentioned in Bridewin D.P.'s answer or transfer the code to client-side and use Javascript to manipulate the rows.
Server-side would look something like this:
Private Sub parameter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim rdbtn As RadioButtonList = DirectCast(sender, RadioButtonList)
Dim selectedText As String = rdbtn.SelectedItem.Text
Dim worksRequiredRowId = SessionManager.getWorksRequiredRowId()
Dim style as String
If selectedText.ToUpper <> ApplicationConstants.conditionSatisfactory.ToUpper Then
style= "table-row"
Else
style= "none"
End If
rdbtn.Items(worksRequiredRowId).Attributes.CssStyle.Add("display", style)
Catch ex As Exception
End Try
End Sub

Removing item from a listbox that is binding to a database table

i. I have a form that allow users add multiple phone numbers using a listbox and two buttons to add to the listbox and to remove from the listbox if they make a mistake and want to correct it before saving to the phone number table in the database.
ii. I have another form which allows them to edit what has been saved, which means they can either remove or add more as the case maybe.
The first (i) works perfectly well while the second (ii) does not. What I discovered is that i can only remove what I added but not what is coming from the database table. How do I do that?
Below is my code sample for the second form (ii):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Contact_Phone(getId)
End If
end sub
Private Sub Contact_Phone(ByVal FK_CID As Integer)
Dim strSQL As String
strSQL = "Select PK_PNID,PN_Number From tblPhoneNumber where FK_CID=" & FK_CID
With cClass
.BindListBox(Me.lbPhone, strSQL, "PN_Number", "PK_PNID")
End With
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Me.lbPhone.Items.Remove(Me.lbPhone.SelectedItem.Text)
Catch ex As Exception
If ex.Message = "Object reference not set to an instance of an object." Then
Exit Sub
End If
Me.lblErr.ForeColor = Drawing.Color.Red
Me.lblErr.Text = ex.Message
End Try
End Sub
When in the form(ii) an user is deleting a phone number, just delete it from the db, so next time when the phone numbers are shown in the form(ii) the deleted phone number will not be there.
If you do not want to make a post back on delete button click(which will erase the user added phone numbers in the list box as it has not been saved to the db till now), then make an AJAX call to the sever passing the values and delete the number.

Datagrid row not being read code

I have the following code...
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.RowUpdating
Dim SocioNumInfo As Integer = CInt(GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text)
MsgBox(SocioNumInfo.ToString)
...
End Sub
Now, this code should read the cell, but it gives me the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The MsgBox is just there for me to check if the data is being read, at the end of the day it should turn that into a parameter so I can add to the DB... but still.. nope nothing. Is that the correct code to read directly from a cell in a choosen row? In the "Protected Sub" area I already tried SelectedIndexChanging and RowEditing, etc... and still nothing. Still throws the error at me.
If I try
Dim SocioNumInfo As String = CStr(GridView1.Rows(GridView1.SelectedRow).Cells(4).Text) it gives me a "cannot be converted to Integer" error.
If no row is selected then GridView1.SelectedIndex is -1. You might have to add a check
If GridView1.SelectedIndex <> -1 Then
' Use GridView1.SelectedIndex here
End If
However, it is easier to access the selected row like this:
Dim row = GridView1.SelectedRow
If Not row Is Nothing AndAlso IsNumeric(row.Cells(2).Text) Then
Dim SocioNumInfo As Integer = CInt(row.Cells(2).Text)
....
End If
Note that the cell index is zero based. row.Cells(4) is in the 5th column.

convert unbound Gridview column values to lowercase at runtime

I have an unbound Gridview that is populated by a Linq to Entities query and would like to convert string values in a particular column to lowercase.
In the Gridview's RowDataBound event, i have tried StrConv(e.Row.Cells(3).Text, VbStrConv.ProperCase) but this doesn't work.
I have also tried StrConv(emp.Name, VbStrConv.ProperCase) in the LiNQ to Entities query but still the Name values returned are to converted to Lower-case.
Protected Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim cellDate As Date
If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
End If
Next
End If
StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
End Sub
As far as I can see, strConv returns a string, which should be used, I think like:
e.Row.Cells(4).Text = StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
have you tried to do this:
string strLower = e.Row.Cells[0].Text.ToLower();
and then use the strLower as the lower case string.

Resources