You should use the int.TryParse to convert to an integer. If the value in the textbox is not able to be converted to an integer you will not get an error
} catch { Message("block 3:There is an error in the entry form required, please contact the Department of Computer", this); } } catch { Message("block 2: ERROR CONTACT YOUR SYSTEM ADMINISTRATOR", this); } PatNo.Text = ""; NameTxtBx.Text = ""; ToTxtBx0.Text = ""; DropDownList1.SelectedIndex = -1; RadioButtonList2.SelectedIndex = -1; } catch { Message(" ERROR 20", this); } } } catch { Message("Error: Make sure you enter the data correctly ", this); } }
Abdul Muqeet
Member
82 Points
432 Posts
Error in converting Text box
Jun 24, 2012 11:39 AM|LINK
In Table the value is Patient_ID_NO INT and its giving error in my code
try
{
string Patient_name = NameTxtBx.Text, Export_TO = ToTxtBx0.Text, repType = RadioButtonList2.SelectedValue ;
int PatNoVal;
PatNoVal = Convert.ToInt32(PatNo.Text);
PatNoVal = int.Parse(PatNo.Text);
System.Nullable<int> idcount = null;
int PatID; <-----------
PatID = Convert.ToInt32(PatID_NO.Text); <------------these lines giving error
PatID = int.Parse(PatID_NO.Text); <-------------
int? replay_To_type = Int16.Parse(DropDownList1.SelectedValue);
try
{
TransDataContext db = new TransDataContext();
try
{
var q = db.Transaction(Patient_name, Export_TO,replay_To_type,null,repType,PatNoVal,PatID,ref idcount);
}
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: Error in converting Text box
Jun 24, 2012 11:41 AM|LINK
You should use the int.TryParse to convert to an integer. If the value in the textbox is not able to be converted to an integer you will not get an error
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx
Space Coast .Net User Group
Abdul Muqeet
Member
82 Points
432 Posts
Re: Error in converting Text box
Jun 24, 2012 11:46 AM|LINK
how can i use int.TryParse with Text box? its not giving error its trowing message from catch cannot insert,
Sage Gu - MS...
Contributor
6693 Points
578 Posts
Microsoft
Re: Error in converting Text box
Jun 25, 2012 07:57 AM|LINK
Hi Abdul,
What's the content do you input in the PatID_NO TextBox?
Regards,
Sage Gu - MSFT
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
bkis1112
Participant
1061 Points
226 Posts
Re: Error in converting Text box
Jun 25, 2012 08:05 AM|LINK
Using int.TryParse to avoid error
int PatID= 0; int.TryParse(PatID_NO.Text, out PatID); if (PatID> 0 )//text box value is integer { //parse success, PatID is change value }Reference: http://msdn.microsoft.com/en-us/library/f02979c7.aspx
Thanks for reading my post
Trần Lê Thành Trung
Abdul Muqeet
Member
82 Points
432 Posts
Re: Error in converting Text box
Jun 25, 2012 08:22 AM|LINK
PatID = Convert.ToInt32(PatID_NO.Text);
PatID = int.Parse(PatID_NO.Text);
DateTime today = Convert.ToDateTime(DateTime.Now.ToShortDateString());
System.Nullable<decimal> idcount = null;
int? replay_To_type = Int16.Parse(DropDownList1.SelectedValue);
try { TransDataContext db = new TransDataContext();
try {
var q = db.Transaction(Patient_name, Export_TO,replay_To_type,null,repType,PatNoVal,PatID,ref idcount);
Abdul Muqeet
Member
82 Points
432 Posts
Re: Error in converting Text box
Jun 25, 2012 08:31 AM|LINK
what shoul i write in if statement
ssjGanesh
Participant
1928 Points
1352 Posts
Re: Error in converting Text box
Jun 25, 2012 08:36 AM|LINK
int PatID;
PatID = Convert.ToInt32(PatID_NO.Text);
These 2 lines are enough to convert the string to int.
Verify the datatype in ur database table whether it is int or not.
and check it is Identity column or not? If it is identity u can't assign value for that.
And post the error msg here .....
Mark as answer,if it helped U!
Abdul Muqeet
Member
82 Points
432 Posts
Re: Error in converting Text box
Jun 25, 2012 08:48 AM|LINK
before data type was numeric,, i changed to int ,,it is not identity column , PATID_NO.Text will be calculate in a class.
from the code its thowing this error message
Rishi Ganesh
Member
586 Points
123 Posts
Re: Error in converting Text box
Jun 25, 2012 08:54 AM|LINK
Hi Abdul,
Use the following.
int PatNoVal;
PatNoVal = Convert.ToInt32(((PatNo.Text != null) ? ((PatNo.Text.ToString().Trim().Length > 0) ? PatNo.Text.ToString().Trim() : "0") : "0"));