Hello All,
Happy New Year.
Thanks.
private void searchButton_Click(object sender, EventArgs e)
{
if (searchComboBox.Text == "ID")
{
try
{
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE ID = '" + searchentryTextBox.Text + "'; ", conn);
ds.Clear();
idTextBox.DataBindings.Clear();
fnameTextBox.DataBindings.Clear();
lnameTextBox.DataBindings.Clear();
phoneTextBox.DataBindings.Clear();
na1TextBox.DataBindings.Clear();
na2TextBox.DataBindings.Clear();
na3TextBox.DataBindings.Clear();
da.Fill(ds);
bs.DataSource = ds.Tables[0];
idTextBox.DataBindings.Add(new Binding("Text", bs, "ID"));
fnameTextBox.DataBindings.Add(new Binding("Text", bs, "fname"));
lnameTextBox.DataBindings.Add(new Binding("Text", bs, "lname"));
phoneTextBox.DataBindings.Add(new Binding("Text", bs, "phone"));
na1TextBox.DataBindings.Add(new Binding("Text", bs, "na1"));
na2TextBox.DataBindings.Add(new Binding("Text", bs, "na2"));
na3TextBox.DataBindings.Add(new Binding("Text", bs, "na3"));
}
catch (Exception ex)
{
MessageBox.Show("Error [" + ex.Message + "]");
}
finally
{
conn.Close();
conn.Dispose();
}
}
else
{
if (searchComboBox.Text == "First Name")
{
try
{
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE FNAME = '" + searchentryTextBox.Text + "'; ", conn);
ds.Clear();
idTextBox.DataBindings.Clear();
fnameTextBox.DataBindings.Clear();
lnameTextBox.DataBindings.Clear();
phoneTextBox.DataBindings.Clear();
na1TextBox.DataBindings.Clear();
na2TextBox.DataBindings.Clear();
na3TextBox.DataBindings.Clear();
da.Fill(ds);
bs.DataSource = ds.Tables[0];
idTextBox.DataBindings.Add(new Binding("Text", bs, "ID"));
fnameTextBox.DataBindings.Add(new Binding("Text", bs, "fname"));
lnameTextBox.DataBindings.Add(new Binding("Text", bs, "lname"));
phoneTextBox.DataBindings.Add(new Binding("Text", bs, "phone"));
na1TextBox.DataBindings.Add(new Binding("Text", bs, "na1"));
na2TextBox.DataBindings.Add(new Binding("Text", bs, "na2"));
na3TextBox.DataBindings.Add(new Binding("Text", bs, "na3"));
}
catch (Exception ex)
{
MessageBox.Show("Error [" + ex.Message + "]");
}
finally
{
conn.Close();
conn.Dispose();
}
}
else
{
if (searchComboBox.Text == "Last Name")
{
try
{
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE LNAME = '" + searchentryTextBox.Text + "'; ", conn);
ds.Clear();
idTextBox.DataBindings.Clear();
fnameTextBox.DataBindings.Clear();
lnameTextBox.DataBindings.Clear();
phoneTextBox.DataBindings.Clear();
na1TextBox.DataBindings.Clear();
na2TextBox.DataBindings.Clear();
na3TextBox.DataBindings.Clear();
da.Fill(ds);
bs.DataSource = ds.Tables[0];
idTextBox.DataBindings.Add(new Binding("Text", bs, "ID"));
fnameTextBox.DataBindings.Add(new Binding("Text", bs, "fname"));
lnameTextBox.DataBindings.Add(new Binding("Text", bs, "lname"));
phoneTextBox.DataBindings.Add(new Binding("Text", bs, "phone"));
na1TextBox.DataBindings.Add(new Binding("Text", bs, "na1"));
na2TextBox.DataBindings.Add(new Binding("Text", bs, "na2"));
na3TextBox.DataBindings.Add(new Binding("Text", bs, "na3"));
}
catch (Exception ex)
{
MessageBox.Show("Error [" + ex.Message + "]");
}
finally
{
conn.Close();
conn.Dispose();
}
}
else
{
if (searchComboBox.Text == "Phone")
{
try
{
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE PHONE = '" + searchphoneTextBox.Text + "'; ", conn);
ds.Clear();
idTextBox.DataBindings.Clear();
fnameTextBox.DataBindings.Clear();
lnameTextBox.DataBindings.Clear();
phoneTextBox.DataBindings.Clear();
na1TextBox.DataBindings.Clear();
na2TextBox.DataBindings.Clear();
na3TextBox.DataBindings.Clear();
da.Fill(ds);
bs.DataSource = ds.Tables[0];
idTextBox.DataBindings.Add(new Binding("Text", bs, "ID"));
fnameTextBox.DataBindings.Add(new Binding("Text", bs, "fname"));
lnameTextBox.DataBindings.Add(new Binding("Text", bs, "lname"));
phoneTextBox.DataBindings.Add(new Binding("Text", bs, "phone"));
na1TextBox.DataBindings.Add(new Binding("Text", bs, "na1"));
na2TextBox.DataBindings.Add(new Binding("Text", bs, "na2"));
na3TextBox.DataBindings.Add(new Binding("Text", bs, "na3"));
}
catch (Exception ex)
{
MessageBox.Show("Error [" + ex.Message + "]");
}
finally
{
conn.Close();
conn.Dispose();
}
}
else
{
MessageBox.Show("Please fill in the search fields.");
}
}
}
}
}
theLizardPosted Jan 11, 2011, 5:47 PM
Unless you are in a situation where stored procedures is the only way because of network traffic or other factors which would create bottle necks on the network then sending an sql with field / values is just as quick as sending an sql to run a stored procedure.
But yes it is good to learn about them though, but also learn when to use them and when not to use them, this should come with experience.
Any one can write programs but few can write them with new methods for old solutions, this should be your goal, find new and better ways to solve what has already been done.
Also by using data adapters and data bound controls you are limiting your knowledge of the underlying methods and procedures for getting data from databases, by all means use these controls but if you want to be really good at programming learn how to create these controls, by doing this you will be able to understand how data bound controls function.
Think outside the square...
Sam HobbsPosted Jan 11, 2011, 7:26 PM
F DPosted Jan 11, 2011, 10:27 AM
Great stuff thanks a bunch.
This is some really good information. I wanted to use stored procedures because I was reading up on the sql injection attacks. I don't need the stored procedure but from my reading it is a secure way from preventing attacks, but I am unsure on how to write the stored procedure. I am a beginner in SQL. Any ideas on the stored procedure? I'm teaching myself all this little by little and want to program in this way. I also appreciate the info on the error in the try & catch statement. I would have never known about that.
All in all thanks very much for your input.
theLizardPosted Jan 10, 2011, 6:12 PM
{
case "ID"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE ID = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "First Name"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE FNAME = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "Last Name"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE LNAME = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "Phone"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE PHONE = '" + searchphoneTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
default:
MessageBox.Show("Please fill in the search fields.");
break;
}
//
all above can be done this way...
string field = null;
switch (searchComboBox.Text)
{
case "ID"://Sql query to retrieve all records from database.
field = "id";
break;
case "First Name"://Sql query to retrieve all records from database.
field = "fname";
break;
case "Last Name"://Sql query to retrieve all ecords from database.
field = "lname";
break;
case "Phone"://Sql query to retrieve all records from database.
field = "phone";
break;
default:
MessageBox.Show("Please fill in the search fields.");
break;
}
if(field != null)
{
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE " + field + " = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
}
else
return;
you may also want to parametrize your searchentryTextBox value to stop sql injection attacks,
There are for's and against for stored procedures do you really need them?
Never give user the real error message in a try catch as you have done, this gives potential hackers an insight into your table structures in database systems.
F DPosted Jan 10, 2011, 12:59 PM
This is how I made the code much simpler. Please let me know what you think. I was also considering converting the SQL statements into STORED PROCEDURES(Would that be a good idea?).
Thanks again!
private void searchButton_Click(object sender, EventArgs e)
{
try
{
switch (searchComboBox.Text)
{
case "ID"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE ID = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "First Name"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE FNAME = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "Last Name"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE LNAME = '" + searchentryTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
case "Phone"://Sql query to retrieve all records from database.
da.SelectCommand = new MySqlCommand("SELECT * FROM tblcontacts WHERE PHONE = '" + searchphoneTextBox.Text + "'; ", conn);
tbltxtFunctions();
break;
default:
MessageBox.Show("Please fill in the search fields.");
break;
}
}
catch (Exception ex)
{
MessageBox.Show("An error has occured. " + ex.Message);
}
finally
{
conn.Close();
conn.Dispose();
}
}
Sam HobbsPosted Jan 8, 2011, 1:44 AM
thiago costaPosted Jan 7, 2011, 6:37 PM
Back up your brains and upload the file :D
F DPosted Jan 7, 2011, 4:28 PM
Roy SPosted Jan 7, 2011, 4:11 PM
switch (searchComboBox.Text)
{
case "ID":
//...
break;
case "First Name":
//...
break;
//...
default:
MessageBox.Show("Please fill in the search fields.");
}