Casting a TextBox Properly?

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();
}
 
S

Scott Allen

Sam:

It is quite possible then that the cell contains more controls than
just the TextBox, and the control you are referencing is not really a
TextBox.

A more robust solution is to give the control an ID and obtain a
reference using FindControl:
http://odetocode.com/Articles/116.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

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();
}
 
K

Karl Seguin

Scott's absolutely right...and to expand on this, spaces and newlines are
converted to LiteralControls....this makes the entire ordinal reference very
brittle, if you add a space between two controls, a new LiteralControl will
be created and your indexs all increment by 1....hence use the
FindControl("controlId") approach for robustness..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Scott Allen said:
Sam:

It is quite possible then that the cell contains more controls than
just the TextBox, and the control you are referencing is not really a
TextBox.

A more robust solution is to give the control an ID and obtain a
reference using FindControl:
http://odetocode.com/Articles/116.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

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.Ra
isePostBackEvent(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[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStre
etNumber;
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();
}
 
G

Guest

Hi Scott Allen,

Thank you. You were correct. Now, I am finding extra space in the textbox
fields when I debug. As well I keep getting the following error message:

Input string was not in a correct format.
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.FormatException: Input string was not in a correct
format.

Source Error:


Line
194: clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;
Line 195:
Line 196: clubcmd.ExecuteNonQuery();
Line 197: clubconn.Close();
Line 198: clubdg.EditItemIndex=-1;


Source File: c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs Line: 196

Stack Trace:


[FormatException: Input string was not in a correct format.]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
Sonar3.Secured.UserGrid.Update_Click(Object sender,
DataGridCommandEventArgs e) in
c:\inetpub\wwwroot\sonar3\secured\usergrid.aspx.cs:196

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()


I don't know which Textbox the error is refering to. Any Ideas?
Scott Allen said:
Sam:

It is quite possible then that the cell contains more controls than
just the TextBox, and the control you are referencing is not really a
TextBox.

A more robust solution is to give the control an ID and obtain a
reference using FindControl:
http://odetocode.com/Articles/116.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

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();
}
 
S

Scott Allen

Hi Scott Allen,

Thank you. You were correct. Now, I am finding extra space in the textbox
fields when I debug. As well I keep getting the following error message:

Input string was not in a correct format.
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.FormatException: Input string was not in a correct
format.

What is the type of the parameter the query expects?
 
G

Guest

I don't know which field is causing the error exception the message comes up
when I set the the command as follows:

clubcmd.ExecuteNonQuery();
Which is correct. I tried turning off some variables and parameters but
some fields are required so I get a seperate exception error.

Below are the fields:
UserPrefix=((TextBox)e.Item.Cells[0].FindControl("UserPrefix"));
string strUserPrefix=UserPrefix.Text;
strUserPrefix.Trim();
UserFName=(TextBox)e.Item.Cells[0].FindControl("UserFName");
string strUserFName=UserFName.Text;
strUserFName.Trim();
UserLName=(TextBox)e.Item.Cells[0].FindControl("UserLName");
string strUserLName=UserLName.Text;
strUserLName.Trim();

UserEmail=(TextBox)e.Item.Cells[1].FindControl("UserEmail");
string strUserEmail=UserEmail.Text;
strUserEmail.Trim();
UserPassword=(TextBox)e.Item.Cells[1].FindControl("UserPassword");
string strUserPassword=UserPassword.Text;
strUserPassword.Trim();
UserAccess=(DropDownList)e.Item.Cells[1].FindControl("AccessID");
string strAccessID=UserAccess.SelectedValue;

UserStreetNumber=(TextBox)e.Item.Cells[2].FindControl("UserStreetNumber");
string strUserStreetNumber=UserStreetNumber.Text;
strUserStreetNumber.Trim();
UserAddress=(TextBox)e.Item.Cells[2].FindControl("UserAddress");
string strUserAddress=UserAddress.Text;
strUserAddress.Trim();
UserCity=(TextBox)e.Item.Cells[2].FindControl("UserCity");
string strUserCity=UserCity.Text;
strUserCity.Trim();
UserProvince=(TextBox)e.Item.Cells[2].FindControl("UserProvince");
string strUserProvince=UserProvince.Text;
strUserProvince.Trim();
UserPostal=(TextBox)e.Item.Cells[2].FindControl("UserPostal");
string strUserPostal=UserPostal.Text;
strUserPostal.Trim();

UserPhone=(TextBox)e.Item.Cells[3].FindControl("UserPhone");
string strUserPhone=UserPhone.Text;
UserFax=(TextBox)e.Item.Cells[3].FindControl("UserFax");
string strUserFax=UserFax.Text;
UserMobile=(TextBox)e.Item.Cells[3].FindControl("UserMobile");;
string strUserMobile=UserMobile.Text;

show=(CheckBox)e.Item.Cells[1].FindControl("show");
bool strShow=show.Checked;
Bookings=(CheckBox)e.Item.Cells[1].FindControl("Bookings");
bool strBookings=Bookings.Checked;
General=(CheckBox)e.Item.Cells[1].FindControl("General");
bool strGeneral=General.Checked;
GuestList=(CheckBox)e.Item.Cells[1].FindControl("GuestList");
bool strGuestList=GuestList.Checked;
BarInfo=(CheckBox)e.Item.Cells[1].FindControl("BarInfo");
bool strBarInfo=BarInfo.Checked;
then I set the connection as follows:

clubconn=new SqlConnection(strclubconn);
clubconn.Open();
clubcmd=new SqlCommand(Globals.UserUpdate_SP,clubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
And then the Parameters:

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserID_PARM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserID_PARM].Value=EditIDupdate;

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_AccessID_PARM,SqlDbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_AccessID_PARM].Value=strAccessID;

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,12));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbType.Char,12));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,SqlDbType.Char,12));
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;

and then finally where I get the exception error:

clubcmd.ExecuteNonQuery();
clubconn.Close();
clubdg.EditItemIndex=-1;
BindData();
 
S

Scott Allen

I don't know which field is causing the error exception the message comes up
when I set the the command as follows:

Sam: I'd investigate those bit fields first. I imagine the runtime is
trying to convert strBarInfo to a bool.
 
G

Guest

Yeah I noticed that and have changed the fields to bool. That isn't the
problem. I'm still looking into other causes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top