G
Guest
I have a DataGrid that is passing information to a stored procedure properly
but the parameters aren't being casted properly. I was woundering if anyone
can tell me how I should properly cast the following:
(TextBox)UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
string strUserPrefix=UserPrefix.Text;
I keep getting the following error and I don't know why because I have
declared the UserPrefix as a textbox using "protected
System.Web.UI.Webcontrols.TextBox UserPrefix" as well as casting it in its
referance.
Here is the error message I get:
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 84: int EditIDupdate=(int)clubdg.DataKeys[(int)e.Item.ItemIndex];
Line 85:
Line 86: UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
Line 87: string strUserPrefix=UserPrefix.Text;
Line 88: UserFName=(TextBox)e.Item.Cells[0].Controls[1];
Source File: c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs Line: 86
Stack Trace:
[InvalidCastException: Specified cast is not valid.]
Sonar3.Secured.UserGrid.Update_Click(Object sender,
DataGridCommandEventArgs e) in
c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs:86
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
The Codebehind is as follows:
protected System.Web.UI.WebControls.TextBox UserPrefix;
protected System.Web.UI.WebControls.TextBox UserFName;
protected System.Web.UI.WebControls.TextBox UserLName;
protected System.Web.UI.WebControls.TextBox UserEmail;
protected System.Web.UI.WebControls.TextBox UserPassword;
protected System.Web.UI.WebControls.TextBox UserStreetNumber;
protected System.Web.UI.WebControls.TextBox UserAddress;
protected System.Web.UI.WebControls.TextBox UserCity;
protected System.Web.UI.WebControls.TextBox UserProvince;
protected System.Web.UI.WebControls.TextBox UserPostal;
protected System.Web.UI.WebControls.TextBox UserPhone;
protected System.Web.UI.WebControls.TextBox UserFax;
protected System.Web.UI.WebControls.TextBox UserMobile;
protected System.Web.UI.WebControls.TextBox UserNote;
protected System.Web.UI.WebControls.TextBox _Date;
protected System.Web.UI.WebControls.CheckBox show;
protected System.Web.UI.WebControls.CheckBox Bookings;
protected System.Web.UI.WebControls.CheckBox General;
protected System.Web.UI.WebControls.CheckBox GuestList;
protected System.Web.UI.WebControls.CheckBox BarInfo;
public void Update_Click(Object sender, DataGridCommandEventArgs e)
{
int EditIDupdate=(int)clubdg.DataKeys[(int)e.Item.ItemIndex];
(TextBox)UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
string strUserPrefix=UserPrefix.Text;
UserFName=(TextBox)e.Item.Cells[0].Controls[1];
string strUserFName=UserFName.Text;
UserLName=(TextBox)e.Item.Cells[0].Controls[2];
string strUserLName=UserLName.Text;
UserEmail=(TextBox)e.Item.Cells[1].Controls[0];
string strUserEmail=UserEmail.Text;
UserPassword=(TextBox)e.Item.Cells[1].Controls[1];
string strUserPassword=UserPassword.Text;
UserStreetNumber=(TextBox)e.Item.Cells[2].Controls[0];
string strUserStreetNumber=UserStreetNumber.Text;
UserAddress=(TextBox)e.Item.Cells[2].Controls[1];
string strUserAddress=UserAddress.Text;
UserCity=(TextBox)e.Item.Cells[2].Controls[2];
string strUserCity=UserCity.Text;
UserProvince=(TextBox)e.Item.Cells[2].Controls[3];
string strUserProvince=UserProvince.Text;
UserPostal=(TextBox)e.Item.Cells[2].Controls[4];
string strUserPostal=UserPostal.Text;
UserPhone=(TextBox)e.Item.Cells[3].Controls[0];
string strUserPhone=UserPhone.Text;
UserFax=(TextBox)e.Item.Cells[3].Controls[1];
string strUserFax=UserFax.Text;
UserMobile=(TextBox)e.Item.Cells[3].Controls[2];
string strUserMobile=UserMobile.Text;
show=(CheckBox)e.Item.Cells[1].Controls[2];
string strShow=show.Text;
Bookings=(CheckBox)e.Item.Cells[1].Controls[3];
string strBookings=Bookings.Text;
General=(CheckBox)e.Item.Cells[1].Controls[4];
string strGeneral=General.Text;
GuestList=(CheckBox)e.Item.Cells[1].Controls[5];
string strGuestList=GuestList.Text;
BarInfo=(CheckBox)e.Item.Cells[1].Controls[6];
string strBarInfo=BarInfo.Text;
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
clubcmd=new SqlCommand(Globals.UserUpdate_SP,clubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPrefix_PARM,SqlDbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlDbType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlDbType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlDbType.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,SqlDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PARM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,SqlDbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,SqlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,SqlDbType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlDbType.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbType.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,SqlDbType.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserMobile_PARM].Value=strUserMobile;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_show_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_show_PARM].Value=strShow;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_Bookings_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;
clubcmd.ExecuteNonQuery();
clubconn.Close();
clubdg.EditItemIndex=-1;
BindData();
}
but the parameters aren't being casted properly. I was woundering if anyone
can tell me how I should properly cast the following:
(TextBox)UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
string strUserPrefix=UserPrefix.Text;
I keep getting the following error and I don't know why because I have
declared the UserPrefix as a textbox using "protected
System.Web.UI.Webcontrols.TextBox UserPrefix" as well as casting it in its
referance.
Here is the error message I get:
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 84: int EditIDupdate=(int)clubdg.DataKeys[(int)e.Item.ItemIndex];
Line 85:
Line 86: UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
Line 87: string strUserPrefix=UserPrefix.Text;
Line 88: UserFName=(TextBox)e.Item.Cells[0].Controls[1];
Source File: c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs Line: 86
Stack Trace:
[InvalidCastException: Specified cast is not valid.]
Sonar3.Secured.UserGrid.Update_Click(Object sender,
DataGridCommandEventArgs e) in
c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs:86
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
The Codebehind is as follows:
protected System.Web.UI.WebControls.TextBox UserPrefix;
protected System.Web.UI.WebControls.TextBox UserFName;
protected System.Web.UI.WebControls.TextBox UserLName;
protected System.Web.UI.WebControls.TextBox UserEmail;
protected System.Web.UI.WebControls.TextBox UserPassword;
protected System.Web.UI.WebControls.TextBox UserStreetNumber;
protected System.Web.UI.WebControls.TextBox UserAddress;
protected System.Web.UI.WebControls.TextBox UserCity;
protected System.Web.UI.WebControls.TextBox UserProvince;
protected System.Web.UI.WebControls.TextBox UserPostal;
protected System.Web.UI.WebControls.TextBox UserPhone;
protected System.Web.UI.WebControls.TextBox UserFax;
protected System.Web.UI.WebControls.TextBox UserMobile;
protected System.Web.UI.WebControls.TextBox UserNote;
protected System.Web.UI.WebControls.TextBox _Date;
protected System.Web.UI.WebControls.CheckBox show;
protected System.Web.UI.WebControls.CheckBox Bookings;
protected System.Web.UI.WebControls.CheckBox General;
protected System.Web.UI.WebControls.CheckBox GuestList;
protected System.Web.UI.WebControls.CheckBox BarInfo;
public void Update_Click(Object sender, DataGridCommandEventArgs e)
{
int EditIDupdate=(int)clubdg.DataKeys[(int)e.Item.ItemIndex];
(TextBox)UserPrefix=(TextBox)e.Item.Cells[0].Controls[0];
string strUserPrefix=UserPrefix.Text;
UserFName=(TextBox)e.Item.Cells[0].Controls[1];
string strUserFName=UserFName.Text;
UserLName=(TextBox)e.Item.Cells[0].Controls[2];
string strUserLName=UserLName.Text;
UserEmail=(TextBox)e.Item.Cells[1].Controls[0];
string strUserEmail=UserEmail.Text;
UserPassword=(TextBox)e.Item.Cells[1].Controls[1];
string strUserPassword=UserPassword.Text;
UserStreetNumber=(TextBox)e.Item.Cells[2].Controls[0];
string strUserStreetNumber=UserStreetNumber.Text;
UserAddress=(TextBox)e.Item.Cells[2].Controls[1];
string strUserAddress=UserAddress.Text;
UserCity=(TextBox)e.Item.Cells[2].Controls[2];
string strUserCity=UserCity.Text;
UserProvince=(TextBox)e.Item.Cells[2].Controls[3];
string strUserProvince=UserProvince.Text;
UserPostal=(TextBox)e.Item.Cells[2].Controls[4];
string strUserPostal=UserPostal.Text;
UserPhone=(TextBox)e.Item.Cells[3].Controls[0];
string strUserPhone=UserPhone.Text;
UserFax=(TextBox)e.Item.Cells[3].Controls[1];
string strUserFax=UserFax.Text;
UserMobile=(TextBox)e.Item.Cells[3].Controls[2];
string strUserMobile=UserMobile.Text;
show=(CheckBox)e.Item.Cells[1].Controls[2];
string strShow=show.Text;
Bookings=(CheckBox)e.Item.Cells[1].Controls[3];
string strBookings=Bookings.Text;
General=(CheckBox)e.Item.Cells[1].Controls[4];
string strGeneral=General.Text;
GuestList=(CheckBox)e.Item.Cells[1].Controls[5];
string strGuestList=GuestList.Text;
BarInfo=(CheckBox)e.Item.Cells[1].Controls[6];
string strBarInfo=BarInfo.Text;
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
clubcmd=new SqlCommand(Globals.UserUpdate_SP,clubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPrefix_PARM,SqlDbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlDbType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlDbType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlDbType.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,SqlDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PARM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,SqlDbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,SqlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,SqlDbType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlDbType.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbType.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,SqlDbType.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserMobile_PARM].Value=strUserMobile;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_show_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_show_PARM].Value=strShow;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_Bookings_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;
clubcmd.ExecuteNonQuery();
clubconn.Close();
clubdg.EditItemIndex=-1;
BindData();
}