tags:

views:

108

answers:

2

Hello all, I am trying to access entity class but getting the error message "No overload method for CreateDoctor takse 3 argument" Here is my code please modify somebody.. DoctorProperty class: public class DoctorProperty //class for doctor table { int _Id; string _Name; string _Address; int _Phone; public int Id { get { return _Id; } set { _Id = value; } } public string Name { get { return _Name; } set { _Name = value; } } public string Address { get { return _Address; } set { _Address = value; } } public int Phone { get { return _Phone; } set { _Phone = value; } } } Doctor DataLayer: public class DoctorDataLayer { public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); public int AddDoctor(DoctorProperty obj) { SqlConnection con = new SqlConnection(str);
SqlCommand com =new SqlCommand("insert into Doctor values('"+obj.Name+"','"+obj.Address+"','"+obj.Phone+"')",con); com.ExecuteNonQuery(); return 0;
} } Doctor BusinessLayer: public class DoctorBusinessLayer {
public void CreateDoctor(DoctorProperty obj) { DoctorDataLayer dl = new DoctorDataLayer(); dl.AddDoctor(obj);
} } AdDoctor.aspx.cs protected void Btnsubmit_Click(object sender, EventArgs e) { DoctorBusinessLayer DB = new DoctorBusinessLayer(); DoctorProperty dp = new DoctorProperty(); DB.CreateDoctor(TxtName.Text, TxtAddress.Text, TxtPhone.Text); } Above highlighted line getting error:

A: 

Your CreateDoctor method only take 1 argument.

public void CreateDoctor(DoctorProperty)

But you're calling it with 3 arguments...

DB.CreateDoctor(string, string, string)

You could solve the problem in 1 way...

Edit: Taking a closer look at your code it seems it would benefit of a rewrite, so here goes:

public class Doctor
{
  // JohannesH: Changed to use auto properties instead.
  public int Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string Phone { get; set; }
}

public static class DoctorDataLayer
{
  public static void AddDoctor(Doctor doctor) // JohannesH: Changed return type from int to void.
  {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // JohannesH: Changed from .ToString() to .ConnectionString

    // JohannesH: Changed to use "using" to make sure everything gets disposed correctly during failure.
    using(var connection = new SqlConnection(connectionString))
    {
      using (var command = new  SqlCommand("insert into doctor values(@name,@address,@phone)", connection))
      {
        // JohannesH: Now using SqlParameters to get rid of sql injection attacks.
        command.Parameters.AddWithValue("@name", doctor.Name);
        command.Parameters.AddWithValue("@address", doctor.Address);
        command.Parameters.AddWithValue("@phone", doctor.Phone);

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
      }
    }
  }
}

public static class DoctorBusinessLayer
{
  public static void CreateDoctor(string name, string address, string phone) 
  {
    DoctorDataLayer.AddDoctor(new Doctor {Name = name, Address = address, Phone = phone});
  }
}
JohannesH
A: 

Change this line :

DB.CreateDoctor(TxtName.Text, TxtAddress.Text, TxtPhone.Text);

With these :

dp.Name = TxtName.Text;
dp.Address = TxtAddress.Text;
dp.Phone = TxtPhone.Text;
DB.CreateDoctor(db);

Here is the reformatted code :

DoctorProperty class: public class DoctorProperty //class for doctor table 
{ 
int _Id; 
string _Name; 
string _Address; 
int _Phone; 

public int Id { get { return _Id; } set { _Id = value; } } 
public string Name { get { return _Name; } set { _Name = value; } } 
public string Address { get { return _Address; } set { _Address = value; } } 
public int Phone { get { return _Phone; } set { _Phone = value; } } 
} 

//Doctor DataLayer: 

public class DoctorDataLayer 
{ 
public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 

public int AddDoctor(DoctorProperty obj) 
{ 
SqlConnection con = new SqlConnection(str);
SqlCommand com =new SqlCommand("insert into Doctor values('"+obj.Name+"','"+obj.Address+"','"+obj.Phone+"')",con); 
com.ExecuteNonQuery(); 
return 0;
} 
} 

//Doctor BusinessLayer: 

public class DoctorBusinessLayer {
public void CreateDoctor(DoctorProperty obj) 
{ 
DoctorDataLayer dl = new DoctorDataLayer(); 
dl.AddDoctor(obj);
} 
} 

//AdDoctor.aspx.cs 

protected void Btnsubmit_Click(object sender, EventArgs e) { 
DoctorBusinessLayer DB = new DoctorBusinessLayer(); 
DoctorProperty dp = new DoctorProperty(); 
dp.Name = TxtName.Text;
dp.Address = TxtAddress.Text;
dp.Phone = TxtPhone.Text;
DB.CreateDoctor(db); 
}
Canavar
In his code, "DoctorProperty.Phone" is an int, so at least you have to convert the string (although it seems like it is a string in the db). And please just put a small note about the sql injection vulnerability... I mean, concatenating a sql statement with text values directly from a textbox?! ;)
JohannesH