Reputation: 439
First of all, I would like to point out that I have searched for a solution to this problem without luck.
So basicly I have a Mysql function in Radform1 witch I want to call from CustomAppointmentEditForm
Radform1
public RadForm1()
{
InitializeComponent();
}
CustomAppointmentEditForm
public CustomAppointmentEditForm()
{
InitializeComponent();
}
The function I want to call from Radform1
private void Update_LeadInfo()
{
try
{
using (MySqlConnection cn = new MySqlConnection(ConString))
{
string UniqueID = textBoxDescription.Text;
string CVR = txtCVR.Text;
string Firma = txtFirma.Text;
string Nummer = txtNummer.Text;
string Addresse = txtAddresse.Text;
string Postnr = txtPostnr.Text;
string By = txtBy.Text;
string Noter = txtNoter.Text;
string Email = txtEmail.Text;
string StartDato = dateStart.Text + " " + timeStart.Text;
string SlutDato = dateEnd.Text + " " + timeEnd.Text;
string Afholdt = radCheckBox1.CheckState.ToString();
if(Afholdt == "Checked")
{
Afholdt = "1";
}
else
{
Afholdt = "0";
}
if(chkAllDay.CheckState == CheckState.Checked)
{
StartDato = dateStart.Text + " " + "00:00";
SlutDato = dateEnd.Text + " " + "00:00";
}
cn.Open();
Console.WriteLine(UniqueID);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn;
cmd.CommandText = "UPDATE Leads SET CVR = ('" + CVR + "'), Firma = ('" + Firma + "'), Nummer = ('" + Nummer + "'), Addresse = ('" + Addresse + "'), Postnr = ('" + Postnr + "'), Bynavn = ('" + By + "'), Noter = ('" + Noter + "'), Afholdt = ('" + Afholdt + "'), Email = ('" + Email + "'), Slut_Dato = ('" + SlutDato + "') WHERE UniqueID = ('" + UniqueID + "');";
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
}
}
catch (Exception Fejl)
{
Console.WriteLine(Fejl);
}
}
I know there is a simple solution to this, thanks in advance
Upvotes: 0
Views: 91
Reputation: 5324
EDIT: I wouldn't use this method there.
Update_LeadInfo
doesn't seem to use any methods from RadForm1
besides those TextBox.Text
objects.
Create another class with paremeters for accessing the Database, e.g.
public static class DatabaseAccess{
private static string ConString = "<SOMECONSTRING>";
public static void Update_LeadInfo(LeadInfo infoObj){ //Don't mix CamelCase and _ Case Notation
using (MySqlConnection cn = new MySqlConnection(ConString))
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn;
cmd.CommandText = "UPDATE Leads SET CVR = ('" + infoObj.CVR + "'), Firma = ('" + infoObj.Firma + "'), Nummer = ('" + infoObj.Nummer + "'), Addresse = ('" + infoObjAddresse + "'), Postnr = ('" + infoObj.Postnr + "'), Bynavn = ('" + infoObj.By + "'), Noter = ('" + infoObj.Noter + "'), Afholdt = ('" + infoObj.Afholdt + "'), Email = ('" + infoObj.Email + "'), Slut_Dato = ('" + infoObj.SlutDato + "') WHERE UniqueID = ('" + infoObj.UniqueID + "');";
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
}
}
}
You LeadInfo
class:
public class LeadInfo{
public string CVR {get;set}
...
public string Afholdt {get;set;}
}
And use Parameterized Queries!
Instead of cmd.CommandText = "UPDATE Leads SET CVR = ('" + infoObj.CVR + "'), Firma = ('" + infoObj.Firma + "'), Nummer = ('" + infoObj.Nummer + "'), Addresse = ('" + infoObjAddresse + "'), Postnr = ('" + infoObj.Postnr + "'), Bynavn = ('" + infoObj.By + "'), Noter = ('" + infoObj.Noter + "'), Afholdt = ('" + infoObj.Afholdt + "'), Email = ('" + infoObj.Email + "'), Slut_Dato = ('" + infoObj.SlutDato + "') WHERE UniqueID = ('" + infoObj.UniqueID + "');";
Use:
string updateCommand = "UPDATE Leads SET CVR = @paramCVR, ..., WHERE UniqueID = @paramUniqueID;"
MySqlCommand m = new MySqlCommand(updateCommand);
m.Parameters.AddWithValue("@paramCVR", infoObj.CVR);
...
m.Parameters.AddWithValue("@paramUniqueID", infoObj.UniqueID);
To get access to another public non-static method, you have to reference it in the class you want to use it.
So when you initialize your CustomAppointmentEditForm
now, you have to pass the RadForm1
as a parameter.
Build as second constructor without the parameter, if you want to call CustomAppointmentEditForm
like before.
Take this as an example:
RadForm1 radObj;
public CustomAppointmentEditForm(RadForm1 radObj)
{
InitializeComponent();
this.radObj = radObj;
}
private void SomeMethod()
{
radObj.Update_LeadInfo();
}
And this is your call from RadForm1
:
CustomAppointmentEditForm custForm = new CustomAppointmentEditForm(this);
Upvotes: 2
Reputation: 612
Firstly, Update_LeadInfo()
seems like business logic, and you should not have business-logic in UI layer. If you move it to separate data access project, you can refactor your code like that:
DataAccessLayer:
class DataAdapter
{
public void UpdateLeads(LeadsInfo info)
{
using (MySqlConnection cn = new MySqlConnection(ConString))
{
cn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn;
cmd.CommandText = "UPDATE Leads SET CVR = ('" + info.CVR + "'), Firma = ('" + info.Firma + "')," /* ... */;
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
}
}
}
UI Layer:
class RadForm1
{
public RadForm1(DataAdapter adapter)
{
if (adapter == null)
throw new ArgumentNullException("adapter");
InitializeComponent();
this.Adapter = adapter
}
private void Update_LeadInfo()
{
this.Adapter.UpdateLeads(new LeadsInfo(
CVR: txtCVR.Text,
Firma: txtFirma.Text,
/* ... */));
}
}
class CustomAppointmentForm
{
public CustomAppointmentForm(DataAdapter adapter)
{
if (adapter == null)
throw new ArgumentNullException("adapter");
InitializeComponent();
this.Adapter = adapter
}
private void Update_LeadInfo()
{
this.Adapter.UpdateLeads(new LeadsInfo(
CVR: txtCVR.Text,
Firma: txtFirma.Text,
/* ... */));
}
}
Upvotes: 1