Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi how can i convert nvarchar to int in asp.net?
i set date to textbox but type of date in my sql is nvarchar
my code is;
C#
PersianCalendar p = new PersianCalendar();
      DateTime mdate = DateTime.Now;
      int pyear = p.GetYear(mdate);
      int pmonth = p.GetMonth(mdate);
      int pday = p.GetDayOfMonth(mdate);
      string pdate = string.Format("{2}/{1}/{0}", pyear, pmonth, pday);
      TextBox2.Text = pdate;
//..................
  SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NezamPezeshki;Integrated Security=True");
        con.Open();
    
        SqlCommand com = new SqlCommand("INSERT INTO shekayat(FileNum,shekayatDate, shakiName, shakiTell, patient, Description) VALUES ( @shd, @shn, @sht, @p, @des,@fn)", con);
        com.Parameters.AddWithValue("@shn", TextBox1.Text);
        com.Parameters.AddWithValue("@shd", TextBox2.Text);
        com.Parameters.AddWithValue("@p", TextBox3.Text);
        com.Parameters.AddWithValue("@sht", TextBox4.Text);
        com.Parameters.AddWithValue("@fn",TextBox13.Text);
        com.Parameters.AddWithValue("@des", TextBox7.Text);
        
 
        Label3.Visible = true;
        int id = Convert.ToInt32(com.ExecuteScalar());
         Label3 .Text  = String.Format("شماره کلاسه {0}", id);

       
        Label3.Text = "success";
        con.Close();
Posted
Updated 3-Jul-12 19:17pm
v2

Either use int.Parse[^] or int.TryParse[^]:
C#
string s = "1234";
int i = int.Parse(s);
...
int j;
if (int.TryParse(s, out j))
   {
   ...
   }


[edit]Improved example - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Rahul Rajat Singh 4-Jul-12 1:27am    
+5.
First thing, why so: type of date in my sql is nvarchar

Second, you can try:
Convert.ToInt32 to convert a string into interger. You can handle the conversion failure as Overflow or Format exception.

Ex:
C#
int convertedInteger = Convert.ToInt32("someString");
// if someString was 1234, convertedInteger = 1234
// if someString was null, convertedInteger = 0

Refer: MSDN: Convert.ToInt32 Method (String)[^]

UPDATE:
my field in sql is nvarchar and i want send date like 12/02/2012 to related field in sql

Your code already have most of it. To have a defined date format sent as a string to a Varchar field:
C#
int pyear = p.GetYear(mdate);
int pmonth = p.GetMonth(mdate);
int pday = p.GetDayOfMonth(mdate);
string pdate = string.Format("{2}/{1}/{0}", pyear, pmonth, pday);
// Now pass this pdate to database for that varchar field. 
 
Share this answer
 
v4
Comments
veusk 4-Jul-12 2:08am    
i did it,but receive bellow error,yet;

Conversion failed when converting the nvarchar value '13/4/1391' to data type int.
Sandeep Mewara 4-Jul-12 3:18am    
Error says issue when you try to convert it into integer which will be.

what you ask is how to put it in varchar which is not an integer and will not throw any error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900