Click here to Skip to main content
15,888,579 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to create a setup file Pin
Expert Coming12-Aug-08 19:58
Expert Coming12-Aug-08 19:58 
AnswerRe: How to create a setup file Pin
Vimalsoft(Pty) Ltd12-Aug-08 20:06
professionalVimalsoft(Pty) Ltd12-Aug-08 20:06 
QuestionCreating WMS Publishing Point. Pin
deepaks312-Aug-08 18:52
deepaks312-Aug-08 18:52 
AnswerRe: Creating WMS Publishing Point. Pin
robbyking15-Sep-08 5:22
robbyking15-Sep-08 5:22 
QuestionC#:OnPaintBackground in Component and User Control Pin
cocoonwls12-Aug-08 14:44
cocoonwls12-Aug-08 14:44 
AnswerRe: C#:OnPaintBackground in Component and User Control Pin
mmikey712-Aug-08 20:28
mmikey712-Aug-08 20:28 
GeneralRe: C#:OnPaintBackground in Component and User Control Pin
cocoonwls15-Aug-08 0:01
cocoonwls15-Aug-08 0:01 
QuestionSQL CE & C# Pin
three.leaf12-Aug-08 14:26
three.leaf12-Aug-08 14:26 
Hi, i'm new in this forum.

I am currently developing a Pocket Application using C# & SQL CE. Now, everything was working fine until just now. When I do any type of operations in the database (insert, update & delete) running the application in the device everything goes right, no errors. But When I check the database, no changes has been made. It seems that everything is working but I do not know if I'm missing something. I didn't noticed this until i was testing my RDA functions, when i realized that the tables didn't have any data of the SQL Server, any idea of how to solve this?


this class creates connections to the SQL CE database.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Windows;
using System.IO;
using CAPocket.Clases;

namespace CAPocket.Clases
{
    class Data
    {
        String cadenaCon;
        public Data()
        {
            cadenaCon = @"Data Source =\Program Files\CAPocket\Sistema.sdf;SSCE:Database Password='pam653045';";
        }

        public SqlCeConnection GetConnection()
        {
            SqlCeConnection con = new SqlCeConnection(cadenaCon);
            try
            {
                con.Open();
                return con;
            }
            catch (SqlCeException ex)
            {
                return null;
            }
        }

        public SqlCeDataAdapter getAdapter(String s_sql)
        {
            SqlCeDataAdapter adp;
            try
            {
                adp = new SqlCeDataAdapter(s_sql, cadenaCon);
                return adp;
            }
            catch (SqlCeException ex)
            { return null; }
        }

    }
}


This code block of a class allows the administrator to add, delete or get all the users registered in the application:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Windows;
using System.IO;
using System.Data;
using CAPocket.Clases;

namespace CAPocket
{
    class GestionUsuarios
    {
        public GestionUsuarios()
        {

        }
        Data cex;
        SqlCeConnection cnn;
        SqlCeCommand cmd;
        //Agrega un nuevo usuario al sistema
        public bool NuevoUsuario(String user, String pass, String tipo)
        {
            cex = new Data();
            cnn = cex.GetConnection();
            if (cnn != null)
            {
                String sql = "insert into Usuarios values ('" + user + "','" + pass + "', '" + tipo + "')";
                cmd = new SqlCeCommand(sql, cnn);
                try
                {
                    cmd.ExecuteNonQuery();
                    return true;
                }
                catch (SqlCeException ex)
                { return false; }
                finally
                { cnn.Close(); }
            }
            else
            { return false; }
        }

        //funcion que verifica si ya existe un usuario con los parametros
        //de nombre y password
        public bool Existe(String user, String pass)
        {
            cex = new Data();
            cnn = cex.GetConnection();
            String sql = "select * from Usuarios where Usuario='" + user + "'";

            //se realiza conexion a base de datos
            //se verifica que sea valida

            if (cnn != null)
            {
                cmd = new SqlCeCommand(sql, cnn);
                try
                {
                    SqlCeDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch(SqlCeException ex)
                { return false; }
                finally
                { cnn.Close(); }
            }
            else
            { return false; }
        }

        //obtiene todos los usuarios y a cual tipo pertenecen
        public DataSet getAllUsers()
        {
            DataSet ds = new DataSet();
            cex = new Data();
            SqlCeDataAdapter adp = cex.getAdapter("Select Usuario, TipoUsuario from Usuarios order by TipoUsuario");
            adp.Fill(ds, "Usuarios");
            return ds;
        }

        //Elimina un usuario enviando el nombre de usuario en 
        //los parametros de la funcion
        public bool EliminarUsuario(String user)
        {
            cex = new Data();
            cnn = cex.GetConnection();
            String sql = "delete from Usuarios where Usuario='" + user + "'";
            if (cnn != null)
            {
                cmd = new SqlCeCommand(sql, cnn);
                try
                {
                    cmd.ExecuteNonQuery();
                    return true;
                }
                catch (SqlCeException ex)
                { return false; }
                finally
                { cnn.Close(); }
            }
            else { return false; }
        }

    }
}


the next code is from the form where you add, delete and show all the users.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Windows;
using System.IO;
using CAPocket.Clases;

namespace CAPocket
{
    public partial class frmUsuarios : Form
    {
        public frmUsuarios()
        {
            InitializeComponent();
        }

        public DataSet dset;
        GestionUsuarios gu;

        public void RefreshGrid()
        {
            //carga los usuarios registrados en el sistema
            //copia los usuarios a un dataset y los envia 
            //al datagrid
            gu = new GestionUsuarios();
            dset = new DataSet();
            dset.Clear();
            dset = gu.getAllUsers();
            
            dataGrid1.DataSource = dset.Tables["Usuarios"];
            dataGrid1.TableStyles.Clear();
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = "Usuarios";

            //Modificando el estilo de la columna de usuario
            DataGridColumnStyle cUsuario = new DataGridTextBoxColumn();
            cUsuario.HeaderText = "Usuario";
            cUsuario.MappingName = "Usuario";
            cUsuario.Width = 75;
            ts.GridColumnStyles.Add(cUsuario);

            //Modificando el estilo de la columnda de tipo de usuario
            DataGridColumnStyle cTipoUsuario = new DataGridTextBoxColumn();
            cTipoUsuario.HeaderText = "Tipo de Usuario";
            cTipoUsuario.MappingName = "TipoUsuario";
            cTipoUsuario.Width = 100;
            ts.GridColumnStyles.Add(cTipoUsuario);

            dataGrid1.TableStyles.Add(ts);
        }

        private void frmUsuarios_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btAgregar_Click(object sender, EventArgs e)
        {
            //busca si el usuario existe
            //si no existe, lo agrega a la base de datos
            //si existe, manda un error
            //el nombre de usuario es inrepetible, no importa
            //si son de diferentes tipos
            GestionUsuarios gu = new GestionUsuarios();
            bool resultado = gu.Existe(txtUsuario.Text, txtPassword.Text);
            if (resultado == false)
            {
                if (radioButton1.Checked == true)
                { resultado = gu.NuevoUsuario(txtUsuario.Text, txtPassword.Text, "Usuario"); }
                if (radioButton2.Checked == true)
                { resultado = gu.NuevoUsuario(txtUsuario.Text, txtPassword.Text,"Administrador"); }
                if (resultado == true)
                { 
                    MessageBox.Show("Usuario agregado exitosamente", "Resultados");
                    RefreshGrid();
                }
                else
                { MessageBox.Show("Error al guardar","Errores"); }
            }
            else
            {
                MessageBox.Show("El usuario ya existe","Errores");
            }

            txtUsuario.Text = "";
            txtPassword.Text = "";
        }

        private void datagrid1_Click(object sender, EventArgs e)
        {
            //selecciona la fila completa
            dataGrid1.Select(dataGrid1.CurrentRowIndex);
            textBox1.Text = dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            //selecciona la fila completa
            dataGrid1.Select(dataGrid1.CurrentRowIndex);
            textBox1.Text = dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();
        }

        private void btEliminar_Click(object sender, EventArgs e)
        {
           gu = new GestionUsuarios();
           bool resultado = gu.EliminarUsuario(textBox1.Text);
           if (resultado == true)
           {
                MessageBox.Show("Usuario eliminado", "Resultados");
                RefreshGrid();
            }
            else
            { MessageBox.Show("Error al eliminar", "Error"); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Hope anyone can help me! Cry | :((
AnswerRe: SQL CE & C# Pin
N a v a n e e t h12-Aug-08 17:15
N a v a n e e t h12-Aug-08 17:15 
GeneralRe: SQL CE & C# Pin
three.leaf13-Aug-08 10:45
three.leaf13-Aug-08 10:45 
QuestionThe DLL file Pin
Silvyster12-Aug-08 13:32
Silvyster12-Aug-08 13:32 
AnswerRe: The DLL file Pin
Christian Graus12-Aug-08 19:38
protectorChristian Graus12-Aug-08 19:38 
AnswerRe: The DLL file Pin
Vimalsoft(Pty) Ltd12-Aug-08 20:00
professionalVimalsoft(Pty) Ltd12-Aug-08 20:00 
QuestionShowing and hiding the console window Pin
Ekjon12-Aug-08 12:37
Ekjon12-Aug-08 12:37 
AnswerRe: Showing and hiding the console window Pin
PIEBALDconsult12-Aug-08 12:49
mvePIEBALDconsult12-Aug-08 12:49 
GeneralRe: Showing and hiding the console window Pin
Ekjon13-Aug-08 6:51
Ekjon13-Aug-08 6:51 
AnswerRe: Showing and hiding the console window Pin
Mark Salsbery12-Aug-08 13:03
Mark Salsbery12-Aug-08 13:03 
GeneralRe: Showing and hiding the console window Pin
Ekjon13-Aug-08 5:30
Ekjon13-Aug-08 5:30 
GeneralRe: Showing and hiding the console window Pin
Mark Salsbery13-Aug-08 6:06
Mark Salsbery13-Aug-08 6:06 
GeneralRe: Showing and hiding the console window Pin
Ekjon13-Aug-08 6:39
Ekjon13-Aug-08 6:39 
GeneralRe: Showing and hiding the console window Pin
Mark Salsbery13-Aug-08 7:03
Mark Salsbery13-Aug-08 7:03 
GeneralRe: Showing and hiding the console window Pin
Ekjon13-Aug-08 7:47
Ekjon13-Aug-08 7:47 
GeneralRe: Showing and hiding the console window Pin
Mark Salsbery13-Aug-08 7:57
Mark Salsbery13-Aug-08 7:57 
GeneralRe: Showing and hiding the console window Pin
Ekjon13-Aug-08 8:13
Ekjon13-Aug-08 8:13 
GeneralRe: Showing and hiding the console window Pin
Mark Salsbery13-Aug-08 8:25
Mark Salsbery13-Aug-08 8:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.