0

I get an error of conversion of nvarchar to int. I also tried parsing but it does not solve my problem. So how can I solve that problem??

this is my stored procedure that has a int parameter :

ALTER PROCEDURE  [dbo].[Student_Registration] @SSID int
AS
SELECt TRoom ,DayWeek,ET ,ST ,LNProfessor ,NProfessor ,Code  ,  TLesson ,'false' AS IsChecked
FROM View_1Registration
WHERE View_1Registration.SubjectStudyId=@SSID

this is my Source code:

   private DataTable GetData()
    {
        string conString = ConfigurationManager.ConnectionStrings["KDUIS-v1ConnectionString"].ConnectionString;
        SqlCommand cmd = new SqlCommand("Student_Registration");
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@SSID", Convert.ToInt32( LblSubjectStudy.Text.Trim()));
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

when i run my source cod i got into this error in this line of code:

                    cmd.Parameters.AddWithValue("@SSID", Convert.ToInt32( LblSubjectStudy.Text.Trim()));

this is an error message:

'Input string was not in a correct format.'
dorsa
  • 85
  • 7
  • 1
    What do you get in LblSubjectStudy.Text.Trim() – Praneet Nadkar Nov 14 '18 at 11:05
  • 3
    This has nothing to do with `nvarchar`. You need to *parse* a *string* into a number. The error may be thrown simply because the input is *not* a number and can't be parsed. For example `ABCD` can't be parsed into a number. – Panagiotis Kanavos Nov 14 '18 at 11:06
  • try this: var x=Convert.ToInt32( LblSubjectStudy.Text.Trim()); cmd.Parameters.AddWithValue("@SSID", x); – shaghayegh sheykholeslami Nov 14 '18 at 11:06
  • @shaghayeghsheykholeslami that's what the OP already did. Putting the call to Convert to another line won't change anything – Panagiotis Kanavos Nov 14 '18 at 11:07
  • 1
    @dorsa don't use `Convert.ToInt32`. At best it calls `int.Parse()` using default settings. If you need to specify a format or locale, you'd have to use `int.Parse` anyway. What does `LblSubjectStudy.Text` contain? Is it really a number or some other string? – Panagiotis Kanavos Nov 14 '18 at 11:09
  • can you try LblSubjectStudy.Text.ToString(); – yusuf hayırsever Nov 14 '18 at 11:09
  • @PraneetNadkar LblSubjectStudy get an integer value – dorsa Nov 14 '18 at 11:19
  • @PanagiotisKanavos I also used the int.parse() but I'm still getting same error – dorsa Nov 14 '18 at 11:22
  • @yusufhayırseverI do it, and I'm still getting same error – dorsa Nov 14 '18 at 11:25
  • @dorsa which is perferctly normal if that label contains *letters* instead of digits. What does `LblSubjectStudy.Text` contain? It's impossible to help without any relevant info. – Panagiotis Kanavos Nov 14 '18 at 11:29
  • @PanagiotisKanavos Thx you so much. my problem soved. i try this code: int ssid; if (Int32.TryParse(LblSubjectStudy.Text.Trim(), out ssid)) { cmd.Parameters.AddWithValue("@SSID", ssid); } – dorsa Nov 14 '18 at 12:02

0 Answers0