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. General Programming
  3. C#
  4. not save my the data

not save my the data

Scheduled Pinned Locked Moved C#
3 Posts 3 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.
  • A Offline
    A Offline
    antonio_dsanchez
    wrote on last edited by
    #1

    I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data

    /////////////////inicio guardar
    //cadena de conexion
    string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
    string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);

    try
    {
    using (SqlCeConnection con = new SqlCeConnection(connectionstring))
    {
    string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";

    	foreach (DataRow row in dt.Rows)
    	{
            SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con);
    		
    		cmd.Parameters.Clear();
    
    		cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString()));
    		cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString()));
    		cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString()));
    		cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString()));
    		cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString()));
    		cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString()));
    		
    		con.Open();
    
    		cmd.ExecuteNonQuery();
            
            con.Close();
       }
      
    }	
    
    MessageBox.Show("Datos Agregados");
    

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    ///////////////fin guardado

    P OriginalGriffO 2 Replies Last reply
    0
    • A antonio_dsanchez

      I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data

      /////////////////inicio guardar
      //cadena de conexion
      string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
      string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
      string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);

      try
      {
      using (SqlCeConnection con = new SqlCeConnection(connectionstring))
      {
      string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";

      	foreach (DataRow row in dt.Rows)
      	{
              SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con);
      		
      		cmd.Parameters.Clear();
      
      		cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString()));
      		cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString()));
      		cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString()));
      		cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString()));
      		cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString()));
      		cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString()));
      		
      		con.Open();
      
      		cmd.ExecuteNonQuery();
              
              con.Close();
         }
        
      }	
      
      MessageBox.Show("Datos Agregados");
      

      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      ///////////////fin guardado

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      How do you know it's not saving the data? I see that you are using the executable path for the location of the database, so I assume that you have it in a root folder of your project. So, when you build, the file is (in all probability) copied over the one in the executable location. You have two possible fixes: 1. Change the file property in Visual Studio so that it only copies when newer. 2. Don't use the executable location. Option 2 is the better choice because you won't be able to update the data in the database once you install it into a protected folder such as inside Program Files. You should look to use AppData to store your file instead.

      This space for rent

      1 Reply Last reply
      0
      • A antonio_dsanchez

        I'm working on dataGrid Save content based on SQL Server data, C # smart devices and I have all the code I do not make mistake but not save data

        /////////////////inicio guardar
        //cadena de conexion
        string partDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        string partBD = System.IO.Path.Combine(partDir, "embarque.sdf");
        string connectionstring = string.Format(@"DataSource={0}; Password ='root'", partBD);

        try
        {
        using (SqlCeConnection con = new SqlCeConnection(connectionstring))
        {
        string ConsultaSQL = "INSERT INTO ENVIO VALUES(@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)";

        	foreach (DataRow row in dt.Rows)
        	{
                SqlCeCommand cmd = new SqlCeCommand(ConsultaSQL, con);
        		
        		cmd.Parameters.Clear();
        
        		cmd.Parameters.AddWithValue("@RAID", Convert.ToString(row\["Raid"\].ToString()));
        		cmd.Parameters.AddWithValue("@NPARTE", Convert.ToString(row\["Nparte"\].ToString()));
        		cmd.Parameters.AddWithValue("@CANTIDAD", Convert.ToString(row\["Cantidad"\].ToString()));
        		cmd.Parameters.AddWithValue("@VENTANA", Convert.ToString(row\["Ventana"\].ToString()));
        		cmd.Parameters.AddWithValue("@LIDER", Convert.ToString(row\["Lider"\].ToString()));
        		cmd.Parameters.AddWithValue("@FECHA", Convert.ToString(row\["Fecha"\].ToString()));
        		
        		con.Open();
        
        		cmd.ExecuteNonQuery();
                
                con.Close();
           }
          
        }	
        
        MessageBox.Show("Datos Agregados");
        

        }
        catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
        ///////////////fin guardado

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        As Pete says, the database location is probably relevant here - you should never store data in the app folder as it tends to work in development, but fail in production. The reason for that is that in production it's installed under "Program Files" (or its x86 equivelant) which is protected from changes by the system to help prevent virus activity. Have a look here: Where should I store my data?[^] - it shows some better places, and how to access them. In addition, it's a poor idea to just us an anonymous INSERT - you should list the columns in the order you are going to INSERT them as this means your code is more resilient to changes in your DB. Use:

        INSERT INTO ENVIO (Raid, Nparte, Cantidad, Ventana, Lider, Fecha) VALUES (@RAID, @NPARTE, @CANTIDAD, @VENTANA, @LIDER, @FECHA)

        (Or similar) instead.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        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