Reputation: 315
Total C# beginner here. I'm trying to update a "Patient" table in an SQL database from a Web Form. I'm calling a "PatientRegistration" method of a WCF service I've written to do so. When a Patient is added, the service returns "True", if it fails, it returns "False".
The app builds, runs and returns "true"... but when I check the database, none of the Patients I've added appear in the table (even after a refresh).
Can someone spot where I might be going wrong? Here is my code for the "database service":
namespace ADOWebApp2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ADODatabaseService" in code, svc and config file together.
public class ADODatabaseService : IADODatabaseService
{
public bool PatientRegistration(string hno, string fname, string lname, int pnum, string address, string email)
{
string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\xxxxx\\Documents\\Visual Studio 2010\\Projects\\ADOWebApp2\\ADOWebApp2\\App_Data\\ADOdb.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connString);
string sqlquery = "select * from Patient";
SqlDataAdapter sqladapter = new SqlDataAdapter();
SqlCommandBuilder cb = new SqlCommandBuilder(sqladapter);
try
{
conn.Open();
sqladapter.SelectCommand = new SqlCommand(sqlquery, conn);
DataSet patient = new DataSet();
sqladapter.Fill(patient, "Patient");
DataRow row = patient.Tables["Patient"].NewRow();
row[0] = hno;
row[1] = fname;
row[2] = lname;
row[3] = pnum;
row[4] = address;
row[5] = email;
sqladapter.Update(patient, "Patient");
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
Upvotes: 3
Views: 525
Reputation: 216273
Just a missing a line in your code...
DataRow row = patient.Tables["Patient"].NewRow();
row[0] = hno;
row[1] = fname;
row[2] = lname;
row[3] = pnum;
row[4] = address;
row[5] = email;
patient.Tables["Patient"].Rows.Add(row); // <- Add the row to the collection
sqladapter.Update(patient, "Patient");
Upvotes: 1