Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrong Format

Status
Not open for further replies.

liamba

Technical User
Jan 6, 2007
21
IE
hi,
im trying to write to a sql server database using the following code

DataRow objRow = objDataSet.Tables["tblMember"].Rows.Find(Session["MemberID"]);

objRow["Name"] = lblMemberName.Text;
objRow["Password"] = txtPassword.Text;
objRow["Address1"] = txtAddress1.Text;
objRow["Address2"] = txtAddress2.Text;
objRow["Address3"] = txtAddress3.Text;
objRow["Telephone"] = txtTelephone.Text;
objRow["Mobile"] = Int32.Parse(txtMobile.Text);
objRow["ConfirmSMS"] = ddlConfirmSMS.Text;
objRow["MembershipType"] = ddlGender.Text;

however i get an error at the txtmobile saying that the input string is in the wrong format. i am storing it in a field with a data type of numeric(18,0).i dont want to change the data type as i need to use it in the format it is stored in.

Any suggestions would be great
 
The maximum value of an integer datatype in SQL Server is 2,147,483,647 (hence the need for your numeric(18,0) data type. I believe the maximum value for a 32 bit integer is the same. If you use Int64.Parse I think you will be alright.

Also, you seem to have been having trouble getting answers. You might want to read this FAQ's: faq222-2244. Good luck,

Alex

Ignorance of certain subjects is a great part of wisdom
 
yeah i understand what your saying....i forgot to add to my above statement that im trying to write to the database while allowing nulls within the txtmobile field in the database. so when nothing is entered in the text box and i try to write this is when the error occurs
 
Hm, you are not going to be able to convert an empty string to an integer that is for sure. Maybe you can add some if logic to make objRow[Mobile] NULL if the text box contains an empty string or non-numeric data?

I try to control my DB access through stored procedures, that might be another thing you could look into.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
binary isnull operator "A ?? B" says return A if it is not null, else B. vb == IsNull(), Oracle == Nvl(), Access = Nz(), it's fairly common as a function. so:

Code:
long bigNum = long.Parse(txtMaybeNull.Text ?? "0");

or, better still:

Code:
long bigNum;
if ( long.TryParse(txtMaybeNull.Text ?? "0", ref bigNum) )
{
  objRow["Mobile"] = bigNum;
}
else
{
 objRow["Mobile"] = null; // 0L/-1L?
}


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top