Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ. (επεξεργασμένο)

Έχω φτιάξει μια φόρμα με TabControl με 4Tabs και όταν πάω στο καθένα χωρίς να γράψω κάτι σε οποιοδήποτε πεδίο μου βγάζει το μήνυμα Data stored success και μου τα βάζει στην Database με την κάθε ονομασία π.χ textBox1.Text και πάει λέγοντας, τι λέτε να φταίει?

Θέλω να συμπληρώνω μερικά πεδία όχι όλα και να πατάω την Αποθήκευση και να βάζει αυτά που έχω καταχώρηση μονο τίποτα άλλο..

 

Κώδικας:

 

>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace TestFormApplication1
{
public partial class Form1 : Form
{
 OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TestDB.mdb");

 public Form1()
 {
	 InitializeComponent();
 }
 private void button2_Click(object sender, EventArgs e)
 {
	 string vsql = string.Format("insert into Users (textBox1,comboBox8,textBox3,textBox4,textBox5,textBox6,comboBox9,textBox8,textBox9,textBox10,textBox11,textBox12,textBox13,textBox14,textBox15,textBox16,textBox17,textBox18,textBox19,textBox20,textBox21,textBox22,textBox23,textBox24,textBox25,textBox26,textBox27,textBox28,textBox29,textBox30,textBox31,textBox32,textBox33,textBox34,textBox35,textBox36,textBox37,textBox38,textBox39,textBox40,textBox41,textBox42,textBox43,textBox44,textBox45,textBox46,textBox47,textBox48,textBox49,textBox50,textBox51,textBox52,textBox53,textBox54,textBox55,textBox56,richTextBox1,richTextBox2,richTextBox3,checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7,checkBox8,checkBox9,checkBox10,checkBox11,checkBox12,checkBox13,checkBox14,checkBox15,checkBox16,checkBox17,checkBox18,checkBox19,checkBox20,comboBox1,comboBox2,comboBox3,comboBox4,comboBox5,comboBox6,comboBox7) values ('" + textBox1.Text + "','" + comboBox8.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + comboBox9.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox19.Text + "','" + textBox20.Text + "','" + textBox21.Text + "','" + textBox22.Text + "','" + textBox23.Text + "','" + textBox24.Text + "','" + textBox25.Text + "','" + textBox26.Text + "','" + textBox27.Text + "','" + textBox28.Text + "','" + textBox29.Text + "','" + textBox30.Text + "','" + textBox31.Text + "','" + textBox32.Text + "','" + textBox33.Text + "','" + textBox34.Text + "','" + textBox35.Text + "','" + textBox36.Text + "','" + textBox37.Text + "','" + textBox38.Text + "','" + textBox39.Text + "','" + textBox40.Text + "','" + textBox41.Text + "','" + textBox42.Text + "','" + textBox43.Text + "','" + textBox44.Text + "','" + textBox45.Text + "','" + textBox46.Text + "','" + textBox47.Text + "','" + textBox48.Text + "','" + textBox49.Text + "','" + textBox50.Text + "','" + textBox51.Text + "','" + textBox52.Text + "','" + textBox53.Text + "','" + textBox54.Text + "','" + textBox55.Text + "','" + textBox56.Text + "','" + richTextBox1.Text + "','" + richTextBox2.Text + "','" + richTextBox3.Text + "','" + checkBox1.Text + "','" + checkBox2.Text + "','" + checkBox3.Text + "','" + checkBox4.Text + "','" + checkBox5.Text + "','" + checkBox6.Text + "','" + checkBox7.Text + "','" + checkBox8.Text + "','" + checkBox9.Text + "','" + checkBox10.Text + "','" + checkBox11.Text + "','" + checkBox12.Text + "','" + checkBox13.Text + "','" + checkBox14.Text + "','" + checkBox15.Text + "','" + checkBox16.Text + "','" + checkBox17.Text + "','" + checkBox18.Text + "','" + checkBox19.Text + "','" + checkBox20.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "','" + comboBox4.Text + "','" + comboBox5.Text + "','" + comboBox6.Text + "','" + comboBox7.Text + "')");
	 OleDbCommand vcom = new OleDbCommand(vsql, vcon);
	 vcom.ExecuteNonQuery();
	 MessageBox.Show("Data stored success");
	 vcom.Dispose();
 }

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

 private void Form1_Load(object sender, EventArgs e)
 {
	 vcon.Open();
 }
}
}

Επεξ/σία από Anti-WQZ
Δημοσ.

Έχω φτιάξει μια φόρμα με TabControl με 4Tabs και όταν πάω στο καθένα χωρίς να γράψω κάτι σε οποιοδήποτε πεδίο μου βγάζει το μήνυμα Data stored success και μου τα βάζει στην Database με την κάθε ονομασία π.χ textBox1.Text και πάει λέγοντας, τι λέτε να φταίει?

Θέλω να συμπληρώνω μερικά πεδία όχι όλα και να πατάω την Αποθήκευση και να βάζει αυτά που έχω καταχώρηση μονο τίποτα άλλο..

 

Κώδικας:

 

>
...

 

καλημέρα, από τον κώδικα που παραθέτεις δε γίνεται να βγει συμπέρασμα για το πρόβλημα που έχεις. Πιθανόν κάποιο event εκτελείται και έχεις την παρενέργεια που περιγράφεις. Αν το project δεν είναι μεγάλο, συμπίεσέ το και ανέβασέ το στο νήμα για να το δω μέσα από το VS.

Δημοσ.

>
string vsql = string.Format("insert into Users (textBox1,comboBox8,textBox3,textBox4,textBox5,textBox6,comboBox9,textBox8,textBox9,textBox10,textBox11,textBox12,textBox13,textBox14,textBox15,textBox16,textBox17,textBox18,textBox19,textBox20,textBox21,textBox22,textBox23,textBox24,textBox25,textBox26,textBox27,textBox28,textBox29,textBox30,textBox31,textBox32,textBox33,textBox34,textBox35,textBox36,textBox37,textBox38,textBox39,textBox40,textBox41,textBox42,textBox43,textBox44,textBox45,textBox46,textBox47,textBox48,textBox49,textBox50,textBox51,textBox52,textBox53,textBox54,textBox55,textBox56,richTextBox1,richTextBox2,richTextBox3,checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7,checkBox8,checkBox9,checkBox10,checkBox11,checkBox12,checkBox13,checkBox14,checkBox15,checkBox16,checkBox17,checkBox18,checkBox19,checkBox20,comboBox1,comboBox2,comboBox3,comboBox4,comboBox5,comboBox6,comboBox7) values ('" + textBox1.Text + "','" + comboBox8.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + comboBox9.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox19.Text + "','" + textBox20.Text + "','" + textBox21.Text + "','" + textBox22.Text + "','" + textBox23.Text + "','" + textBox24.Text + "','" + textBox25.Text + "','" + textBox26.Text + "','" + textBox27.Text + "','" + textBox28.Text + "','" + textBox29.Text + "','" + textBox30.Text + "','" + textBox31.Text + "','" + textBox32.Text + "','" + textBox33.Text + "','" + textBox34.Text + "','" + textBox35.Text + "','" + textBox36.Text + "','" + textBox37.Text + "','" + textBox38.Text + "','" + textBox39.Text + "','" + textBox40.Text + "','" + textBox41.Text + "','" + textBox42.Text + "','" + textBox43.Text + "','" + textBox44.Text + "','" + textBox45.Text + "','" + textBox46.Text + "','" + textBox47.Text + "','" + textBox48.Text + "','" + textBox49.Text + "','" + textBox50.Text + "','" + textBox51.Text + "','" + textBox52.Text + "','" + textBox53.Text + "','" + textBox54.Text + "','" + textBox55.Text + "','" + textBox56.Text + "','" + richTextBox1.Text + "','" + richTextBox2.Text + "','" + richTextBox3.Text + "','" + checkBox1.Text + "','" + checkBox2.Text + "','" + checkBox3.Text + "','" + checkBox4.Text + "','" + checkBox5.Text + "','" + checkBox6.Text + "','" + checkBox7.Text + "','" + checkBox8.Text + "','" + checkBox9.Text + "','" + checkBox10.Text + "','" + checkBox11.Text + "','" + checkBox12.Text + "','" + checkBox13.Text + "','" + checkBox14.Text + "','" + checkBox15.Text + "','" + checkBox16.Text + "','" + checkBox17.Text + "','" + checkBox18.Text + "','" + checkBox19.Text + "','" + checkBox20.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "','" + comboBox4.Text + "','" + comboBox5.Text + "','" + comboBox6.Text + "','" + comboBox7.Text + "')");

 

Έχεις ονομάσει τα πεδία στη βάση "textBox1,comboBox8,textBox3,textBox4,textBox5" κτλ;;;;;

 

Επίσης διάβασε για τα parameters (OleDbParameter) γιατί το παρακάτω εκθέτει υπερβολικά τον κώδικά σου για Sql Injection....

>
,'" + textBox37.Text + "','

Δημοσ.

Έχεις pm. Όντως υπήρχε εκ παραδρομής event πάνω στο tab control που σου προκαλούσε το πρόβλημα.

Ο φιλος που σου αναφέρει να προσέξεις για sql injection έχει απόλυτο δίκιο. Πρέπει οπωσδήποτε να γράψεις κώδικα για data validation.

Δημοσ.

Γενικότερα η προσέγγιση γράφω το query σε ένα string με τα values καρφωτά σπάζοντας το και κολλώντας τα ενδιάμεσα είναι ο,τι χειρότερο.

 

Θα μπορούσα να σου πω ότι οκ για ένα απλό project που κάνεις για την πλάκα σου δεν τρέχει τίποτα.

Όμως όχι. Είναι μια κακιά και λάθος συνήθεια που αν κολλήσεις μετά γίνεται ένα βουνό.

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...