Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. datatype conversion error

datatype conversion error

Scheduled Pinned Locked Moved ASP.NET
helpannouncement
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    varsh12
    wrote on last edited by
    #1

    string Code = "select Code from ds1 "
    string s;
    using (SqlCommand command = new SqlCommand(Code, conn))
    {

            s = (string)command.ExecuteScalar();
            
        }
        string update = " update ds1 set name='asd' where Code='" + s + "'";
    

    In above code give the datatype conversion error. e.g. :

    Unable to cast object of type 'System.Int64' to type 'System.String'.

    I declared code column as a bigint datatype.

    P V G S 4 Replies Last reply
    0
    • V varsh12

      string Code = "select Code from ds1 "
      string s;
      using (SqlCommand command = new SqlCommand(Code, conn))
      {

              s = (string)command.ExecuteScalar();
              
          }
          string update = " update ds1 set name='asd' where Code='" + s + "'";
      

      In above code give the datatype conversion error. e.g. :

      Unable to cast object of type 'System.Int64' to type 'System.String'.

      I declared code column as a bigint datatype.

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      string update = " update ds1 set name='asd' where Code='" + s.ToString() + "'";

      Parwej Ahamad g.parwez@gmail.com

      V 1 Reply Last reply
      0
      • V varsh12

        string Code = "select Code from ds1 "
        string s;
        using (SqlCommand command = new SqlCommand(Code, conn))
        {

                s = (string)command.ExecuteScalar();
                
            }
            string update = " update ds1 set name='asd' where Code='" + s + "'";
        

        In above code give the datatype conversion error. e.g. :

        Unable to cast object of type 'System.Int64' to type 'System.String'.

        I declared code column as a bigint datatype.

        V Offline
        V Offline
        Vimalsoft Pty Ltd
        wrote on last edited by
        #3

        What Datatype is the Field "Code" in the Database?

        Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za

        V 1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          What Datatype is the Field "Code" in the Database?

          Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za

          V Offline
          V Offline
          varsh12
          wrote on last edited by
          #4

          hi! sir I declared

          bigint

          datatype.

          V 1 Reply Last reply
          0
          • P Parwej Ahamad

            string update = " update ds1 set name='asd' where Code='" + s.ToString() + "'";

            Parwej Ahamad g.parwez@gmail.com

            V Offline
            V Offline
            varsh12
            wrote on last edited by
            #5

            As-salam-wa-alaikum! this gives same error as mentioned above code. error message appeared on

            s=(string)command.executescaler();

            this line.

            1 Reply Last reply
            0
            • V varsh12

              string Code = "select Code from ds1 "
              string s;
              using (SqlCommand command = new SqlCommand(Code, conn))
              {

                      s = (string)command.ExecuteScalar();
                      
                  }
                  string update = " update ds1 set name='asd' where Code='" + s + "'";
              

              In above code give the datatype conversion error. e.g. :

              Unable to cast object of type 'System.Int64' to type 'System.String'.

              I declared code column as a bigint datatype.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              The ExecuteScalar method returns an Int64 boxed in an object. You can only unbox this value as an Int64 value: long code = (long)command.ExecuteScalar(); If you want this value as a string, you use the ToString method: s = code.ToString(); Now, to answer the next question that you would have asked: As the code field is a bigint, you should not use apostrophes around the value in the update query.

              Despite everything, the person most likely to be fooling you next is yourself.

              V 1 Reply Last reply
              0
              • V varsh12

                hi! sir I declared

                bigint

                datatype.

                V Offline
                V Offline
                Vimalsoft Pty Ltd
                wrote on last edited by
                #7

                then it Should look like this

                    string Code = "select Code from ds1 " ;
                    **int** s; 
                    using (SqlCommand command = new SqlCommand(Code, conn))
                    {
                      s = **(int)**command.ExecuteScalar();
                    }
                
                    string update = " update ds1 set name='asd' where Code='" + s + "'";
                

                Am not Sure what you are doing, but if i were to write your type of code, i would do it like this

                 public int GetData() 
                   {
                      int Code;
                
                      String strCon = "blablablablablablabla";
                
                      SqlConnection con = new SqlConnection(strCon);
                
                      SqlCommand cmdselect = new SqlCommand();
                
                      cmdselect.CommandText = "select Code from ds1";
                
                      cmdselect.Connection = con;
                
                      try
                      {
                          con.Open();
                
                          Code = (int)cmdselect.ExecuteNonQuery();
                
                      }
                       catch(SqlException)
                      {
                           throw; 
                      }
                       finally
                      {
                          con.Close();
                      }
                
                      return Code;
                       
                   }
                

                hope it Helps

                Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za

                1 Reply Last reply
                0
                • V varsh12

                  string Code = "select Code from ds1 "
                  string s;
                  using (SqlCommand command = new SqlCommand(Code, conn))
                  {

                          s = (string)command.ExecuteScalar();
                          
                      }
                      string update = " update ds1 set name='asd' where Code='" + s + "'";
                  

                  In above code give the datatype conversion error. e.g. :

                  Unable to cast object of type 'System.Int64' to type 'System.String'.

                  I declared code column as a bigint datatype.

                  S Offline
                  S Offline
                  smarttom99
                  wrote on last edited by
                  #8

                  Change the line s = (string)command.ExecuteScalar(); to s = command.ExecuteScalar().ToString();

                  V 1 Reply Last reply
                  0
                  • G Guffa

                    The ExecuteScalar method returns an Int64 boxed in an object. You can only unbox this value as an Int64 value: long code = (long)command.ExecuteScalar(); If you want this value as a string, you use the ToString method: s = code.ToString(); Now, to answer the next question that you would have asked: As the code field is a bigint, you should not use apostrophes around the value in the update query.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    V Offline
                    V Offline
                    varsh12
                    wrote on last edited by
                    #9

                    Thanx a lot! my problem has been solved. Actually I want to discuss one more problem. I have a column which contains comlete path of a filename. My requirement is that i want to fetch only filename in running mode. I use this following query, but it is not give the output.

                    string code = "select * from path";
                    string s;
                    using (SqlCommand command = new SqlCommand(code, conn))
                    {
                    conn.Open();
                    SqlDataReader dr;
                    dr = command.ExecuteReader();
                    dr.Read();
                    string s1 = dr.GetValues(0);
                    s=s1
                    conn.Close();
                    }
                    string filename = Path.GetFileNameWithoutExtension(s);
                    SqlDataAdapter da = new SqlDataAdapter(filename, conn);
                    da.Fill(ds, "a");
                    DataList1.DataSource = ds.Tables["a"];
                    DataList1.DataBind();
                    }

                    thanx again

                    G 1 Reply Last reply
                    0
                    • V varsh12

                      Thanx a lot! my problem has been solved. Actually I want to discuss one more problem. I have a column which contains comlete path of a filename. My requirement is that i want to fetch only filename in running mode. I use this following query, but it is not give the output.

                      string code = "select * from path";
                      string s;
                      using (SqlCommand command = new SqlCommand(code, conn))
                      {
                      conn.Open();
                      SqlDataReader dr;
                      dr = command.ExecuteReader();
                      dr.Read();
                      string s1 = dr.GetValues(0);
                      s=s1
                      conn.Close();
                      }
                      string filename = Path.GetFileNameWithoutExtension(s);
                      SqlDataAdapter da = new SqlDataAdapter(filename, conn);
                      da.Fill(ds, "a");
                      DataList1.DataSource = ds.Tables["a"];
                      DataList1.DataBind();
                      }

                      thanx again

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      ahmad25 wrote:

                      SqlDataAdapter da = new SqlDataAdapter(filename, conn);

                      The filename isn't an SQL query, is it? Also, the connection is closed. What is it that you are trying to do here really?

                      Despite everything, the person most likely to be fooling you next is yourself.

                      V 1 Reply Last reply
                      0
                      • S smarttom99

                        Change the line s = (string)command.ExecuteScalar(); to s = command.ExecuteScalar().ToString();

                        V Offline
                        V Offline
                        varsh12
                        wrote on last edited by
                        #11

                        thanx a lot! your query is correct.

                        1 Reply Last reply
                        0
                        • G Guffa

                          ahmad25 wrote:

                          SqlDataAdapter da = new SqlDataAdapter(filename, conn);

                          The filename isn't an SQL query, is it? Also, the connection is closed. What is it that you are trying to do here really?

                          Despite everything, the person most likely to be fooling you next is yourself.

                          V Offline
                          V Offline
                          varsh12
                          wrote on last edited by
                          #12

                          I have a column which contain a complete Path. Like-->: d:\folder\folder1\pic.jpg and so on: I want to print only filename like "pic" and show them in a datalist. in datalist i take linkbutton To display all value. hopefully, I think u understand.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups