Error al agregar datos en una base de datos de Access
Publicado por Alejandro (6 intervenciones) el 31/05/2019 02:18:09
Hola, les paso a explicar. Soy nuevo en lo que respecta a programación en C# y como desafio y proyecto personal me puse la meta de hacer un asistente personal. Este asistente se conecta a una base de datos de Access. En el menú de los comandos tengo distintos botones que conducen a distintas tablas de la base de datos; sociales, apps, carpetas, paginas webs y sistema. El problema es que cuando el usuario quiere agregar el comando que desee, ya sea de sociales, apps, carpetas, paginas webs o sistema, siempre se termina agregando en la tabla de sociales.
Aqui el codigo:
Cualquiera que pueda brindar una ayuda se lo agradezco.
Aqui el codigo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using WpfAV.Properties;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;
namespace WpfAV
{
/// <summary>
/// Lógica de interacción para VentanaComandos.xaml
/// </summary>
public partial class VentanaComandos : Window
{
public ArrayList listaComandos = new ArrayList();
OleDbConnection con;
OleDbDataAdapter adapS, adapC, adapA, adapPW, adapSis;
OleDbCommandBuilder cmdb;
OleDbCommand cmd;
OleDbDataReader reader;
DataSet ds;
string tipo = "Sociales";
bool CeldaIngresada = false;
public VentanaComandos()
{
InitializeComponent();
CargarDatos();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
OcultarId();
}
private void BtnCerrar_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void CargarDatos()
{
if (tipo == "Sociales")
{
Cargar_CmdSociales();
}
else if (tipo == "Apps")
{
Cargar_CmdApps();
}
else if (tipo == "Carpetas")
{
Cargar_CmdCarpetas();
}
else if (tipo == "Paginas webs")
{
Cargar_CmdWebs();
}
else
{
Cargar_CmdSistema();
}
}
void Cargar_CmdSociales()
{
try
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
adapS = new OleDbDataAdapter("SELECT Id, Comando, Accion, Respuesta FROM CmdSociales", con);
ds = new DataSet();
adapS.Fill(ds);
dataGridP.ItemsSource = ds.Tables[0].DefaultView;
con.Close();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
void Cargar_CmdApps()
{
try
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
adapA = new OleDbDataAdapter("SELECT Id, Comando, Accion, Respuesta FROM CmdApps", con);
ds = new DataSet();
adapA.Fill(ds);
dataGridP.ItemsSource = ds.Tables[0].DefaultView;
con.Close();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
void Cargar_CmdCarpetas()
{
try
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
adapC = new OleDbDataAdapter("SELECT Id, Comando, Accion, Respuesta FROM CmdCarpetas", con);
ds = new DataSet();
adapC.Fill(ds);
dataGridP.ItemsSource = ds.Tables[0].DefaultView;
con.Close();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
void Cargar_CmdWebs()
{
try
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
adapPW = new OleDbDataAdapter("SELECT Id, Comando, Accion, Respuesta FROM CmdWebs", con);
ds = new DataSet();
adapPW.Fill(ds);
dataGridP.ItemsSource = ds.Tables[0].DefaultView;
con.Close();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
void Cargar_CmdSistema()
{
try
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
adapSis = new OleDbDataAdapter("SELECT Id, Comando, Accion, Respuesta FROM CmdSistema", con);
ds = new DataSet();
adapSis.Fill(ds);
dataGridP.ItemsSource = ds.Tables[0].DefaultView;
con.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void BtnSociales_Click(object sender, RoutedEventArgs e)
{
Cargar_CmdSociales();
OcultarId();
}
private void BtnCarpetas_Click(object sender, RoutedEventArgs e)
{
Cargar_CmdCarpetas();
OcultarId();
}
private void BtnApps_Click(object sender, RoutedEventArgs e)
{
Cargar_CmdApps();
OcultarId();
}
private void BtnWebs_Click(object sender, RoutedEventArgs e)
{
Cargar_CmdWebs();
OcultarId();
}
private void BtnSistema_Click(object sender, RoutedEventArgs e)
{
Cargar_CmdSistema();
OcultarId();
}
private void BtnAddComandos_Click(object sender, RoutedEventArgs e)
{
try
{
AgregarComandosBD();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
void AgregarComandosBD()
{
if (tipo == "Sociales")
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
cmd = new OleDbCommand(string.Format("INSERT INTO CmdSociales (Comando, Accion, Respuesta) values ('{0}','{1}','{2}')", tbxComando.Text, tbxRutaAccion.Text, tbxRespuesta.Text), con);
cmd.ExecuteNonQuery();
con.Close();
ActualizarTabla();
CargarDatos();
LimpiarDatos();
}
else if (tipo == "Carpetas")
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
cmd = new OleDbCommand(string.Format("INSERT INTO CmdCarpetas (Comando, Accion, Respuesta) values ('{0}','{1}','{2}')", tbxComando.Text, tbxRutaAccion.Text, tbxRespuesta.Text), con);
cmd.ExecuteNonQuery();
con.Close();
ActualizarTabla();
CargarDatos();
LimpiarDatos();
}
else if (tipo == "Apps")
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
cmd = new OleDbCommand(string.Format("INSERT INTO CmdApps (Comando, Accion, Respuesta) values ('{0}','{1}','{2}')", tbxComando.Text, tbxRutaAccion.Text, tbxRespuesta.Text), con);
cmd.ExecuteNonQuery();
con.Close();
ActualizarTabla();
CargarDatos();
LimpiarDatos();
}
else if (tipo == "Paginas webs")
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
cmd = new OleDbCommand(string.Format("INSERT INTO CmdWebs (Comando, Accion, Respuesta) values ('{0}','{1}','{2}')", tbxComando.Text, tbxRutaAccion.Text, tbxRespuesta.Text), con);
cmd.ExecuteNonQuery();
con.Close();
ActualizarTabla();
CargarDatos();
LimpiarDatos();
}
else
{
con = new OleDbConnection(Settings.Default.conexion);
con.Open();
cmd = new OleDbCommand(string.Format("INSERT INTO CmdSistema (Comando, Accion, Respuesta) values ('{0}','{1}','{2}')", tbxComando.Text, tbxRutaAccion.Text, tbxRespuesta.Text), con);
cmd.ExecuteNonQuery();
con.Close();
ActualizarTabla();
CargarDatos();
LimpiarDatos();
}
OcultarId();
}
void LimpiarDatos()
{
tbxComando.Text = string.Empty;
tbxRutaAccion.Text = string.Empty;
tbxRespuesta.Text = string.Empty;
}
void ActualizarTabla()
{
try
{
if (tipo == "Sociales")
{
cmdb = new OleDbCommandBuilder(adapS);
adapS.Update(ds);
}
else if (tipo == "Carpetas")
{
cmdb = new OleDbCommandBuilder(adapC);
adapC.Update(ds);
}
else if (tipo == "Apps")
{
cmdb = new OleDbCommandBuilder(adapA);
adapA.Update(ds);
}
else if (tipo == "Paginas webs")
{
cmdb = new OleDbCommandBuilder(adapPW);
adapPW.Update(ds);
}
else
{
cmdb = new OleDbCommandBuilder(adapSis);
adapSis.Update(ds);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
private void BtnDeleteComando_Click(object sender, RoutedEventArgs e)
{
BorrarDatos();
ActualizarTabla();
CargarDatos();
OcultarId();
}
void BorrarDatos()
{
List<DataRow> TheRows = new List<DataRow>();
for (int i = 0; i < dataGridP.SelectedItems.Count; i++)
{
Object o = dataGridP.SelectedItems[i];
if (o != CollectionView.NewItemPlaceholder)
{
DataRowView r = (DataRowView)o;
TheRows.Add(r.Row);
}
}
foreach (DataRow r in TheRows)
{
int x = ds.Tables[0].Rows.IndexOf(r);
ds.Tables[0].Rows[x].Delete();
}
}
void OcultarId()
{
dataGridP.Columns[0].Visibility = Visibility.Hidden;
}
private void DataGridP_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
CeldaIngresada = true;
}
private void DataGridP_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (CeldaIngresada == true)
{
ActualizarTabla();
CargarDatos();
OcultarId();
}
}
}
}
Cualquiera que pueda brindar una ayuda se lo agradezco.
Valora esta pregunta


0