By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Casting a TextBox Properly?

P: n/a
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(O bject source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)

System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,SqlD bType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlDb Type.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlDb Type.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlDb Type.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,Sq lDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PAR M,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,Sql DbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDbT ype.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,Sq lDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,SqlD bType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlDb Type.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbTy pe.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,SqlD bType.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,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbTy pe.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlDb Type.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbTy pe.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;

clubcmd.ExecuteNonQuery();
clubconn.Close();
clubdg.EditItemIndex=-1;
BindData();
}
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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/

On Tue, 15 Mar 2005 07:51:01 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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.c s:86

System.Web.UI.WebControls.DataGrid.OnUpdateComman d(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)

System.Web.UI.WebControls.LinkButton.System.Web.U I.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,Sql DbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlD bType.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,S qlDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PA RM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,Sq lDbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDb Type.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,S qlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,Sql DbType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlD bType.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbT ype.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,Sql DbType.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,SqlDb Type.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlD bType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;

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


Nov 19 '05 #2

P: n/a
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" <sc***@nospam.odetocode.com> wrote in message
news:gj********************************@4ax.com...
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/

On Tue, 15 Mar 2005 07:51:01 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
I have a DataGrid that is passing information to a stored procedure properlybut the parameters aren't being casted properly. I was woundering if anyonecan 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 itsreferance.

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 aboutthe 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.c s:86


System.Web.UI.WebControls.DataGrid.OnUpdateComman d(DataGridCommandEventArgs

e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source, EventArgse)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)


System.Web.UI.WebControls.LinkButton.System.Web.U I.IPostBackEventHandler.Ra

isePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,Sql DbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlD bType.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,S qlDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword
;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PA RM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStre
etNumber;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,Sq lDbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDb Type.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,S qlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince
;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,Sql DbType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;
clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlD bType.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbT ype.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,Sql DbType.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,SqlDb Type.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlD bType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;

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

Nov 19 '05 #3

P: n/a
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(Com mandBehavior
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(O bject source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)

System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()
I don't know which Textbox the error is refering to. Any Ideas?
"Scott Allen" wrote:
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/

On Tue, 15 Mar 2005 07:51:01 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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.c s:86

System.Web.UI.WebControls.DataGrid.OnUpdateComman d(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)

System.Web.UI.WebControls.LinkButton.System.Web.U I.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,Sql DbType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlD bType.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlD bType.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,S qlDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PA RM,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,Sq lDbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDb Type.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,S qlDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,Sql DbType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlD bType.Int,9));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFax_PARM,SqlDbT ype.Char,9));
clubcmd.Parameters[Globals.User_tbl_UserFax_PARM].Value=strUserFax;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,Sql DbType.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,SqlDb Type.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlD bType.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_BarInfo_PARM].Value=strBarInfo;

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


Nov 19 '05 #4

P: n/a
On Tue, 15 Mar 2005 10:09:02 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #5

P: n/a
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,SqlDbTyp e.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserID_PARM].Value=EditIDupdate;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPrefix_PARM,SqlD bType.Char,5));
clubcmd.Parameters[Globals.User_tbl_UserPrefix_PARM].Value=strUserPrefix;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserFName_PARM,SqlDb Type.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserFName_PARM].Value=strUserFName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserLName_PARM,SqlDb Type.Char,150));
clubcmd.Parameters[Globals.User_tbl_UserLName_PARM].Value=strUserLName;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserEmail_PARM,SqlDb Type.Char,255));
clubcmd.Parameters[Globals.User_tbl_UserEmail_PARM].Value=strUserEmail;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPassword_PARM,Sq lDbType.Char,16));
clubcmd.Parameters[Globals.User_tbl_UserPassword_PARM].Value=strUserPassword;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_AccessID_PARM,SqlDbT ype.Char,5));
clubcmd.Parameters[Globals.User_tbl_AccessID_PARM].Value=strAccessID;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserStreetNumber_PAR M,SqlDbType.Int,4));
clubcmd.Parameters[Globals.User_tbl_UserStreetNumber_PARM].Value=strUserStreetNumber;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserAddress_PARM,Sql DbType.Char,200));
clubcmd.Parameters[Globals.User_tbl_UserAddress_PARM].Value=strUserAddress;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserCity_PARM,SqlDbT ype.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserCity_PARM].Value=strUserCity;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserProvince_PARM,Sq lDbType.Char,20));
clubcmd.Parameters[Globals.User_tbl_UserProvince_PARM].Value=strUserProvince;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPostal_PARM,SqlD bType.Char,10));
clubcmd.Parameters[Globals.User_tbl_UserPostal_PARM].Value=strUserPostal;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserPhone_PARM,SqlDb Type.Int,12));
clubcmd.Parameters[Globals.User_tbl_UserPhone_PARM].Value=strUserPhone;

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

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_UserMobile_PARM,SqlD bType.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,SqlDbT ype.Bit,1));
clubcmd.Parameters[Globals.User_tbl_Bookings_PARM].Value=strBookings;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_General_PARM,SqlDbTy pe.Bit,1));
clubcmd.Parameters[Globals.User_tbl_General_PARM].Value=strGeneral;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_GuestList_PARM,SqlDb Type.Bit,1));
clubcmd.Parameters[Globals.User_tbl_GuestList_PARM].Value=strGuestList;

clubcmd.Parameters.Add(new
SqlParameter(Globals.User_tbl_BarInfo_PARM,SqlDbTy pe.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();

"Scott Allen" wrote:
On Tue, 15 Mar 2005 10:09:02 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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?

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

Nov 19 '05 #6

P: n/a
On Tue, 15 Mar 2005 11:15:02 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #7

P: n/a
Yeah I noticed that and have changed the fields to bool. That isn't the
problem. I'm still looking into other causes.
"Scott Allen" wrote:
On Tue, 15 Mar 2005 11:15:02 -0800, "I am Sam"
<Ia****@discussions.microsoft.com> wrote:
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.

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

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.