I had made a registration page of the following code & when ever i run the code for enter data i am getting no errors , but when i refresh my remote database the field/information is not updated, I don't know where i am making mistake...
Here i am calling out my connection string from my web.config file.
public string GetConnectionString()
{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RN_DBConnectionString"].ConnectionString;
}
below is the code, i am getting no errors in it but my remote database is been not updated. Iam doing something wrong..????
private void ExecuteInsert(string FName, string LName, string EID, string Password, string RPassword,
string Organization, string WPhone, string CPhone, string Country,
string City, string State, string Address)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO RN_DB.dbo.Table (FName, LName, EID, Password, RPassword, Organization, WPhone,CPhone,Country, City, State, Address) VALUES "
+ " (#FName,#LName,#EID,#Password,#RPassword,#Organization,#WPhone,#CPhone,#Country,#City,#State,#Addess)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[12];
//param[0] = new SqlParameter("#id", SqlDbType.NVarChar, 50);
param[0] = new SqlParameter("#FName", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("#LName", SqlDbType.NVarChar, 50);
param[2] = new SqlParameter("#EID", SqlDbType.NVarChar, 50);
param[3] = new SqlParameter("#Password", SqlDbType.NVarChar, 50);
param[4] = new SqlParameter("#RPassword", SqlDbType.NVarChar, 50);
param[5] = new SqlParameter("#Organization", SqlDbType.NVarChar, 50);
param[6] = new SqlParameter("#WPhone", SqlDbType.NVarChar, 50);
param[7] = new SqlParameter("#CPhone", SqlDbType.NVarChar, 50);
param[8] = new SqlParameter("#Country", SqlDbType.NVarChar, 50);
param[9] = new SqlParameter("#City", SqlDbType.NVarChar, 50);
param[10] = new SqlParameter("#State", SqlDbType.NVarChar, 50);
param[11] = new SqlParameter("#Address", SqlDbType.Text);
param[0].Value = FName;
param[1].Value = LName;
param[2].Value = EID;
param[3].Value = Password;
param[4].Value = RPassword;
param[5].Value = Organization;
param[6].Value = WPhone;
param[7].Value = CPhone;
param[8].Value = City;
param[9].Value = Country;
param[10].Value = State;
param[11].Value = Address;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
}
finally
{
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Pass.Text == RPass.Text)
{
Guid newGUID = Guid.NewGuid();
//call the method to execute insert to the database
ExecuteInsert(FName.Text,
LName.Text,
EID.Text, Pass.Text, RPass.Text, Org.Text, WPhone.Text, CPhone.Text,
Country.Text,
City.Text, State.Text, Address.Text);
Response.Write("Record was successfully added!");
}
else
{
Response.Write("Password's didnot match");
Pass.Focus();
}
You probably have an error message, but you lose it here:
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
}
Your msg local variable gets the message you need, but you do not show it anywhere. You need to do something: either show it somewhere or throw the exception further.
Related
i'm working to my lending management system and I want to add data from my database coming to two different windows form.
here's my code to the first windows form.
private void button1_Click(object sender, EventArgs e)
{
if (RequiredEntry() == true)
{
return;
}
try
{
SqlConnection cn = new SqlConnection("Data Source=DESKTOP-79MM8LM;Initial Catalog=lend;Integrated Security=True");
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Open();
//if (textBox13.Text.Trim() != textBox26.Text.Trim())
if (textBox13. Text != textBox26.Text)
{
string sSQL = "insert into costumerinfo(name,address,contactNo,occupation,occupationAddress,salary,birthDate,birthPlace,workYears,civilStatus,gender,age,cmName,cmAge,cmAddress,cmContactNo,cmOccupation,cmOccupatioAdd,cmSalary,cm_Name,cm_Age,cm_Address,cm_ContactNo,cm_Occupation,cm_OccupationAdd,cm_Salary) values(#d1,#d2,#d3,#d4,#d5,#d6,#d7,#d8,#d9,#d10,#d11,#d12,#d13,#d14,#d15,#d16,#d17,#d18,#d19,#d20,#d21,#d22,#d23,#d24,#25,#26)";
SqlCommand cmd = new SqlCommand(sSQL, cn);
//new client
;
SqlParameter name = new SqlParameter("#d1", SqlDbType.Char, 10);
name.Value = textBox1.Text.ToString();
cmd.Parameters.Add(name);
SqlParameter address = new SqlParameter("#d2", SqlDbType.VarChar, 50);
address.Value = textBox2.Text.ToString();
cmd.Parameters.Add(address);
SqlParameter contactNo = new SqlParameter("#d3", SqlDbType.Char, 10);
contactNo.Value = textBox3.Text.ToString();
cmd.Parameters.Add(contactNo);
SqlParameter occupation = new SqlParameter("#d4", SqlDbType.Char, 50);
occupation.Value = textBox4.Text.ToString();
cmd.Parameters.Add(occupation);
SqlParameter occupationAddress = new SqlParameter("#d5", SqlDbType.VarChar, 50);
occupationAddress.Value = textBox5.Text.ToString();
cmd.Parameters.Add(occupationAddress);
SqlParameter salary = new SqlParameter("#d6", SqlDbType.Char, 10);
salary.Value = textBox6.Text.ToString();
cmd.Parameters.Add(salary);
SqlParameter birthDate = new SqlParameter("#d7", SqlDbType.VarChar, 10);
birthDate.Value = maskedTextBox1.Text.ToString();
cmd.Parameters.Add(birthDate);
SqlParameter birthPlace = new SqlParameter("#d8", SqlDbType.Char, 50);
birthPlace.Value = textBox8.Text.ToString();
cmd.Parameters.Add(birthPlace);
SqlParameter workYears = new SqlParameter("#d9", SqlDbType.Char, 2);
workYears.Value = textBox9.Text.ToString();
cmd.Parameters.Add(workYears);
SqlParameter civilStatus = new SqlParameter("#d10", SqlDbType.Char, 10);
civilStatus.Value = comboBox1.Text.ToString();
cmd.Parameters.Add(civilStatus);
SqlParameter gender = new SqlParameter("#d11", SqlDbType.Char, 10);
gender.Value = comboBox2.Text.ToString();
cmd.Parameters.Add(gender);
SqlParameter age = new SqlParameter("#d12", SqlDbType.Char, 2);
age.Value = textBox12.Text.ToString();
cmd.Parameters.Add(age);
//co-maker 1
SqlParameter cmName = new SqlParameter("#d13", SqlDbType.Char, 2);
cmName.Value = textBox13.Text.ToString();
cmd.Parameters.Add(cmName);
SqlParameter cmAge = new SqlParameter("#d14", SqlDbType.VarChar, 50);
cmAge.Value = textBox14.Text.ToString();
cmd.Parameters.Add(cmAge);
SqlParameter cmAddress = new SqlParameter("#d15", SqlDbType.VarChar, 50);
cmAddress.Value = textBox15.Text.ToString();
cmd.Parameters.Add(cmAddress);
SqlParameter cmContactNo = new SqlParameter("#d16", SqlDbType.VarChar, 10);
cmContactNo.Value = textBox16.Text.ToString();
cmd.Parameters.Add(cmContactNo);
SqlParameter cmOccupation = new SqlParameter("#d17", SqlDbType.Char, 20);
cmOccupation.Value = textBox17.Text.ToString();
cmd.Parameters.Add(cmOccupation);
SqlParameter cmOccupatioAdd = new SqlParameter("#d18", SqlDbType.VarChar, 50);
cmOccupatioAdd.Value = textBox18.Text.ToString();
cmd.Parameters.Add(cmOccupatioAdd);
SqlParameter cmSalary = new SqlParameter("#d19", SqlDbType.VarChar, 10);
cmSalary.Value = textBox19.Text.ToString();
cmd.Parameters.Add(cmSalary);
//co-maker 2
SqlParameter cm_Name = new SqlParameter("#d20", SqlDbType.Char, 10);
cm_Name.Value = textBox26.Text.ToString();
cmd.Parameters.Add(cm_Name);
SqlParameter cm_Age = new SqlParameter("#d21", SqlDbType.VarChar, 10);
cm_Age.Value = textBox25.Text.ToString();
cmd.Parameters.Add(cm_Age);
SqlParameter cm_Address = new SqlParameter("#d22", SqlDbType.VarChar, 50);
cm_Address.Value = textBox23.Text.ToString();
cmd.Parameters.Add(cm_Address);
SqlParameter cm_ContactNo = new SqlParameter("#d23", SqlDbType.VarChar, 20);
cm_ContactNo.Value = textBox24.Text.ToString();
cmd.Parameters.Add(cm_ContactNo);
SqlParameter cm_Occupation = new SqlParameter("#d24", SqlDbType.VarChar, 50);
cm_Occupation.Value = textBox22.Text.ToString();
cmd.Parameters.Add(cm_Occupation);
SqlParameter cm_OccupationAdd = new SqlParameter("#d25", SqlDbType.VarChar, 50);
cm_OccupationAdd.Value = textBox21.Text.ToString();
cmd.Parameters.AddWithValue("#25", textBox21.Text);
//cmd.Parameters.Add(cm_OccupationAdd);
SqlParameter cm_Salary = new SqlParameter("#d26", SqlDbType.VarChar, 2);
cm_Salary.Value = textBox20.Text.ToString();
cmd.Parameters.AddWithValue("#26", textBox20.Text);
//cmd.Parameters.Add(cm_Salary);
int temp = 0;
temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
cn.Close();
MessageBox.Show("New client added Successfully!");
}
else
{
MessageBox.Show("data not added!");
return;
}
}
else
{`enter code here`
MessageBox.Show("Name of two Co-Maker are the same", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Data Error");
return;
}
}
and here my second windows form
private void button1_Click(object sender, EventArgs e)
{
if (RequiredEntry() == true)
{
return;
}
try
{
SqlConnection cn = new SqlConnection("Data Source=DESKTOP-79MM8LM;Initial Catalog=lend;Integrated Security=True");
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Open();
//if (textBox13.Text.Trim() != textBox26.Text.Trim())
if (String.IsNullOrEmpty(comboBox1.Text))
{
string sSQL = "insert into costumerinfo(name, amount, interest, daysOfPayment, payPerDay, totalAmount, processingFee, insurance, notarial) values(#d1,#d2,#d3,#d4,#d5,#d6,#d7,#d8)";
SqlCommand cmd = new SqlCommand(sSQL, cn);
//new client
//SqlParameter clientID = new SqlParameter("#d1", SqlDbType.Char, 10);
//clientID.Value = textBox1.Text.ToString();
//cmd.Parameters.Add(clientID);
SqlParameter name = new SqlParameter("#d1", SqlDbType.Char, 10);
name.Value = comboBox1.Text.ToString();
cmd.Parameters.Add(name);
SqlParameter amount = new SqlParameter("#d2", SqlDbType.VarChar, 50);
amount.Value = textBox2.Text.ToString();
cmd.Parameters.Add(amount);
SqlParameter interest = new SqlParameter("#d3", SqlDbType.Char, 10);
interest.Value = textBox3.Text.ToString();
cmd.Parameters.Add(interest);
SqlParameter daysOfPayment = new SqlParameter("#d4", SqlDbType.Char, 50);
daysOfPayment.Value = textBox4.Text.ToString();
cmd.Parameters.Add(daysOfPayment);
SqlParameter payPerDay = new SqlParameter("#d5", SqlDbType.VarChar, 50);
payPerDay.Value = label10.Text.ToString();
cmd.Parameters.Add(payPerDay);
SqlParameter totalAmount = new SqlParameter("#d6", SqlDbType.VarChar, 50);
totalAmount.Value = label11.Text.ToString();
cmd.Parameters.Add(totalAmount);
SqlParameter processingFee = new SqlParameter("#d7", SqlDbType.Char, 10);
processingFee.Value = textBox7.Text.ToString();
cmd.Parameters.Add(processingFee);
SqlParameter insurance = new SqlParameter("#d8", SqlDbType.VarChar, 10);
insurance.Value = textBox8.Text.ToString();
cmd.Parameters.Add(insurance);
SqlParameter notarial = new SqlParameter("#d9", SqlDbType.Char, 50);
notarial.Value = textBox9.Text.ToString();
cmd.Parameters.Add(notarial);
int temp = 0;
temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
cn.Close();
MessageBox.Show("New transaction added Successfully!");
}
else
{
MessageBox.Show("data not added!");
return;
}
}
else
{
MessageBox.Show("Name of two Co-Maker are the same", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Data Error");
return;
}
}
but here I got in my database
[the new data was adding but create new line][1]
[i want to add data align with name][2]
[1]: https://i.stack.imgur.com/B6Gb2.png
[2]: https://i.stack.imgur.com/QaLov.png
can you help me thanks
What you want, is to insert from one and update from the other.
SQL insert command -> The INSERT INTO statement is used to insert new records in a table.
SQL update command -> The UPDATE statement is used to modify the existing records in a table.
So your algorithm should be something like this:
Select from the table to see if the row exists
select * from costumerinfo where name = #name
If it does not exist, then insert
insert into costumerinfo
If it does exist, update the relevant values
update customerinfo set ... where name = #name
You can of course change the second form and directly call
string sSQL = "update costumerinfo set amount=#d1, interest=#d2, daysOfPayment = #d3, payPerDay = #d4, totalAmount= $d5, processingFee=#d6, insurance #d7, notarial #d8) values(#d1,#d2,#d3,#d4,#d5,#d6,#d7,#d8) where name = name=#d9";
I'm creating a registration form using using Visual Studio 2015, but when I run my code some problem occurs. I receive the error as stated bellow:
I could not found the problem with my code:
private void execution(string RNumber, string fname, string lname, string Password, string Gender, string CPassword, string Email)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO Table(RNumber, fname, lname, Password, Gender, CPassword, Email) VALUES "
+ " (#RNumber, #fname, #lname, #Password, #Gender, #CPassword, #Email)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] pram = new SqlParameter[7];
pram[0] = new SqlParameter("#RNumber", SqlDbType.VarChar, 50);
pram[1] = new SqlParameter("#fname", SqlDbType.VarChar, 50);
pram[2] = new SqlParameter("#lname", SqlDbType.VarChar, 50);
pram[3] = new SqlParameter("#Password", SqlDbType.VarChar, 50);
pram[4] = new SqlParameter("#Gender", SqlDbType.VarChar, 50);
pram[5] = new SqlParameter("#CPassword", SqlDbType.VarChar, 50);
pram[6] = new SqlParameter("#Email", SqlDbType.VarChar, 50);
pram[0].Value = RNumber;
pram[1].Value = fname;
pram[2].Value = lname;
pram[3].Value = Password;
pram[4].Value = Gender;
pram[5].Value = CPassword;
pram[6].Value = Email;
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex_msg)
{
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
//Here will be fially elements
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
protected void Buttonsubmit_Click(object sender, EventArgs e)
{
if (TextBoxregstr.Text == "")
{
Response.Write("Please complete the form.");
}
else
{
execution(TextBoxregstr.Text, TextBoxfirst.Text, TextBoxlast.Text, TextBoxpswrd.Text, TextBoxcnfrmpswrd.Text, TextBoxgender.Text, TextBoxemail.Text);
Confirm.Visible = true;
TextBoxfirst.Text = "";
TextBoxlast.Text = "";
TextBoxpswrd.Text = "";
TextBoxgender.Text = "";
TextBoxcnfrmpswrd.Text = "";
TextBoxemail.Text = "";
TextBoxregstr.Text = "";
}
}
As the error indicates,
Incorrect syntax near the keyword 'Table'
It happens because TABLE is a reserved keyword for T-SQL. your query should enclose TABLE in square brackets
string sql = "INSERT INTO [Table]
Better way to handle is you change the name and use a more descriptive word for the table
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] pram = new SqlParameter[7];
pram[0] = new SqlParameter("#fname", SqlDbType.VarChar, 50);
pram[1] = new SqlParameter("#lname", SqlDbType.VarChar, 50);
pram[2] = new SqlParameter("#dob", SqlDbType.VarChar, 50);
pram[3] = new SqlParameter("#gender", SqlDbType.Char, 10);
pram[4] = new SqlParameter("#fathername", SqlDbType.VarChar, 50);
pram[5] = new SqlParameter("#contact", SqlDbType.Int, 100);
pram[6] = new SqlParameter("#address", SqlDbType.VarChar, 50);
pram[0].Value = fname;
pram[1].Value = lname;
pram[2].Value = dob;
pram[3].Value = gender;
pram[4].Value = fathername;
pram[5].Value = contact;
pram[6].Value = address;
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex_msg)
{
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
Error received:
Failed to convert parameter value from a String to a Int32
You are probably passing a value that can't be parsed into an int for this parameter:
pram[5] = new SqlParameter("#contact", SqlDbType.Int, 100);
Check what you are passing here:
pram[5].Value = contact;
If contact is a string then do something like:
int contactNumber;
pram[5].Value = int.TryParse(contact, out contactNumber) ? contactNumber : 0;
This is my aspx.cs page,these codes should insert the values into the database,but presently its not going good,its not inserting into database,i am not getting any errors but insertion is not happening,i have no idea what is going wrong.IS there an possibility of SqlException? or any other issues?
protected void btnSkipSubmit_Click(object sender, EventArgs e)
{
int random = 0;
bool isValidInt = int.TryParse(txtrandom.Text, out random);
//string dummmy = "D";
//int dum = 0;
Patient p = new Patient();
//PatientBill pb = new PatientBill();
myConnection obj1 = new myConnection();
DateTime sdt = DateTime.Now;
// string a;
string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
string cmdString = "";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(cmdString, con);
SqlTransaction transaction;
if (isValidInt)
{
for (int i = 0; i < random; i++)
{
//a = obj1.fnSkipPatient(p);
string dummmy = "D";
int dum = 0;
//DateTime sdt = DateTime.Now;
cmdString = "INSERT INTO Patient_Data(PatientID,PatientName,F_H_G,F_H_GName,AgeOnRegn,Email,ContactNo,Gender,Married,AddressLine1,AddressLine2,City,PinCode,Religion,Occupation,RegTime,VisitDate,CurrDept,NextDept,PayID,PayDet1,PayDet2,PayDet3,PayValidity,Archived,UpdateUser,UpdateShift,UpdateDate,LocID,AddressLine3,Remark) VALUES (#PatientID,#PatientName,#F_H_G,#F_H_GName,#AgeOnRegn,#Email,#ContactNo,#Gender,#Married,#AddrLine1,#AddrLine2,#City,#PinCode,#Religion,#Occupation,#RegTime,#VisitDate,#CurrDept,#NextDept,#PayID,#PayDet1,#PayDet2,#PayDet3,#PayValidity,#Archived,#UpdateUser,#UpdateShift,#UpdateDate,#LocID,#AddrLine3,#Remark)";
con = new SqlConnection(str);
try
{
//log.Debug("Entering fnAddPatient method-Function to add a new patient into the database,generate registration bill");
con.Open();
transaction = con.BeginTransaction();
cmd = new SqlCommand(cmdString, con, transaction);
cmd.Parameters.Add("#PatientID", SqlDbType.VarChar, 12);
cmd.Parameters["#PatientID"].Value = p.HospitalNo;
cmd.Parameters.Add("#PatientName", SqlDbType.VarChar, 30);
cmd.Parameters["#PatientName"].Value = dummmy;
cmd.Parameters.Add("#F_H_G", SqlDbType.VarChar, 1);
cmd.Parameters["#F_H_G"].Value = dummmy;
cmd.Parameters.Add("#F_H_GName", SqlDbType.VarChar, 30);
cmd.Parameters["#F_H_GName"].Value = dummmy;
cmd.Parameters.Add("#AgeOnRegn", SqlDbType.Int);
cmd.Parameters["#AgeOnRegn"].Value = dum;
cmd.Parameters.Add("#Email", SqlDbType.VarChar, 40);
cmd.Parameters["#Email"].Value = dummmy;
cmd.Parameters.Add("#ContactNo", SqlDbType.VarChar, 12);
cmd.Parameters["#ContactNo"].Value = dummmy;
cmd.Parameters.Add("#Gender", SqlDbType.VarChar, 1);
cmd.Parameters["#Gender"].Value = dummmy;
cmd.Parameters.Add("#Married", SqlDbType.VarChar, 1);
cmd.Parameters["#Married"].Value = dummmy;
cmd.Parameters.Add("#AddrLine1", SqlDbType.VarChar, 100);
cmd.Parameters["#AddrLine1"].Value = dummmy;
cmd.Parameters.Add("#AddrLine2", SqlDbType.VarChar, 100);
cmd.Parameters["#AddrLine2"].Value = dummmy;
cmd.Parameters.Add("#AddrLine3", SqlDbType.VarChar, 100);
cmd.Parameters["#AddrLine3"].Value = dummmy;
cmd.Parameters.Add("#City", SqlDbType.VarChar, 20);
cmd.Parameters["#City"].Value = dummmy;
cmd.Parameters.Add("#PinCode", SqlDbType.Int);
cmd.Parameters["#PinCode"].Value = dum;
cmd.Parameters.Add("#Religion", SqlDbType.VarChar, 20);
cmd.Parameters["#Religion"].Value = dummmy;
cmd.Parameters.Add("#Occupation", SqlDbType.VarChar, 20);
cmd.Parameters["#Occupation"].Value = dummmy;
cmd.Parameters.Add("#RegTime", SqlDbType.DateTime);
cmd.Parameters["#RegTime"].Value = sdt;
cmd.Parameters.Add("#VisitDate", SqlDbType.DateTime);
cmd.Parameters["#VisitDate"].Value = sdt;
cmd.Parameters.Add("#CurrDept", SqlDbType.Int);
cmd.Parameters["#CurrDept"].Value = dum;
cmd.Parameters.Add("#NextDept", SqlDbType.Int);
cmd.Parameters["#NextDept"].Value = dum;
cmd.Parameters.Add("#PayId", SqlDbType.VarChar, 2);
cmd.Parameters["#PayId"].Value = dummmy;
cmd.Parameters.Add("#PayDet1", SqlDbType.VarChar, 15);
cmd.Parameters["#PayDet1"].Value = dummmy;
cmd.Parameters.Add("#PayDet2", SqlDbType.VarChar, 50);
cmd.Parameters["#PayDet2"].Value = dummmy;
cmd.Parameters.Add("#PayDet3", SqlDbType.VarChar, 15);
cmd.Parameters["#PayDet3"].Value = dummmy;
cmd.Parameters.Add("#PayValidity", SqlDbType.DateTime);
cmd.Parameters["#PayValidity"].Value = sdt;
cmd.Parameters.Add("#Archived", SqlDbType.VarChar, 1);
cmd.Parameters["#Archived"].Value = dummmy;
cmd.Parameters.Add("#UpdateUser", SqlDbType.VarChar, 20);
cmd.Parameters["#UpdateUser"].Value = dummmy;
cmd.Parameters.Add("#UpdateShift", SqlDbType.Int);
cmd.Parameters["#UpdateShift"].Value = dum;
cmd.Parameters.Add("#UpdateDate", SqlDbType.DateTime);
cmd.Parameters["#UpdateDate"].Value = sdt;
cmd.Parameters.Add("#PatientCount", SqlDbType.Int);
cmd.Parameters["#PatientCount"].Value = dum;
cmd.Parameters.Add("#LocId", SqlDbType.VarChar, 2);
cmd.Parameters["#LocId"].Value = dummmy;
cmd.Parameters.Add("#Remark", SqlDbType.VarChar, 100);
cmd.Parameters["#Remark"].Value = dummmy;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
string result = cmd.ExecuteNonQuery().ToString();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
}
}
Move cmd.Parameters.Clear(); after cmd.ExecuteNonQuery()
Try with cmd.ExecuteNonQuery() only.
Also note that if your isValidInt is false, your query will not be executed.. So, first check whether your isValidInt is true. Better you change your logic
You must commit changes in transaction and also cosider transaction.Rollback(); in the catch
I think you are missing transaction.Commit(); after cmd.ExecuteNonQuery();.
try
{
con.Open();
transaction = con.BeginTransaction();
cmd = new SqlCommand(cmdString, con, transaction);
cmd.Parameters.AddWithValue("#PatientID", p.HospitalNo);
// Continue your usual work
cmd.Parameters.AddWithValue("#Remark", dummmy);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
transaction.Commit();
cmd.Parameters.Clear();
}
catch (Exception ex)
{
transaction.Rollback();
}
finally
{
con.Close();
}
Something similar to above code should work for you.
Hope it helps.
execute the following code in server management studio....
GRANT INSERT ON [dbo].[Patient_Data] TO public
hope it works...
1)Make Sure that the connection string is valid.
2)If you close and open the sql server management studio the database will be switched over to "master", you have to change the control to your DB.You can see a dropdownbox in the sql editor tool bar where you can select the DB that where the current query should executed.
May I start off by saying this is my 1st assigned database project in 10 years... and 1st time doing it in C#. I am "simply" trying to insert form data into an Oracle table using OleDB.
I keep getting "ORA-00936: missing expression". Below is my code... any idea what's missing?
public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}
private void executeInsert(string EventType, string EventSubType, string DeptName, string EventDate, string Duration, string EventName, string EventAdd, string WardNo, string Program, string NumAtt, string StTime, string EndTime, string MngName, string RecKeeper)
{
OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) VALUES "
+ "(#EventType, #EventSubType, #DeptName, TO_DATE(#EventDate, 'Month dd, YYYY'), #Duration, #EventName, #EventAdd, #WardNo, #Program, #NumAtt, TO_DATE(#StTime, 'HH:MI:SS PM'), TO_DATE(#EndTime, 'HH:MI:SS PM'), #MngName, #RecKeeper)";
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbParameter[] param = new OleDbParameter[14];
param[0] = new OleDbParameter("#EventType", OleDbType.VarChar, 25);
param[1] = new OleDbParameter("#EventSubType", OleDbType.VarChar, 80);
param[2] = new OleDbParameter("#DeptName", OleDbType.VarChar, 240);
param[3] = new OleDbParameter("#EventDate", OleDbType.Date);
param[4] = new OleDbParameter("#Duration", OleDbType.Numeric);
param[5] = new OleDbParameter("#EventName", OleDbType.VarChar, 80);
param[6] = new OleDbParameter("#EventAdd", OleDbType.VarChar, 150);
param[7] = new OleDbParameter("#WardNo", OleDbType.VarChar, 25);
param[8] = new OleDbParameter("#Program", OleDbType.VarChar, 150);
param[9] = new OleDbParameter("#NumAtt", OleDbType.Numeric);
param[10] = new OleDbParameter("#StTime", OleDbType.Date);
param[11] = new OleDbParameter("#EndTime", OleDbType.Date);
param[12] = new OleDbParameter("#MngName", OleDbType.VarChar, 150);
param[13] = new OleDbParameter("#RecKeeper", OleDbType.VarChar, 150);
param[0].Value = EventType;
param[1].Value = EventSubType;
param[2].Value = DeptName;
param[3].Value = EventDate;
param[4].Value = Duration;
param[5].Value = EventName;
param[6].Value = EventAdd;
param[7].Value = WardNo;
param[8].Value = Program;
param[9].Value = NumAtt;
param[10].Value = StTime;
param[11].Value = EndTime;
param[12].Value = MngName;
param[13].Value = RecKeeper;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception ex) { throw ex; }
finally
{
conn.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);
TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();
executeInsert(rbEventType.SelectedItem.Text, ddVolType.SelectedItem.Text,
txtDept.Text, txtEventDate.Text, meetDuration, txtEventName.Text,
txtEventAdd.Text, ddWard.SelectedItem.Value, txtSBPlan.Text, txtNumVol.Text,
txtStTime.Text, txtEndTime.Text, txtEventMgr.Text, txtRecording.Text);
}
UPDATE to code:
public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}
private void executeInsert(string EventType, string EventSubType, string DeptName, string EventDate, string Duration, string EventName, string EventAdd, string WardNo, string Program, string NumAtt, string StTime, string EndTime, string MngName, string RecKeeper)
{
OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) VALUES "
+ "(#EventType, #EventSubType, #DeptName, TO_DATE(#EventDate, 'Month dd, YYYY'), #Duration, #EventName, #EventAdd, #WardNo, #Program, #NumAtt, TO_DATE(#StTime, 'HH:MI:SS PM'), TO_DATE(#EndTime, 'HH:MI:SS PM'), #MngName, #RecKeeper)";
try
{
conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(new OleDbParameter[]
{
new OleDbParameter("#EventType", EventType),
new OleDbParameter("#EventSubType", EventSubType),
new OleDbParameter("#DeptName", DeptName),
new OleDbParameter("#EventDate", EventDate),
new OleDbParameter("#Duration", Duration),
new OleDbParameter("#EventName", EventName),
new OleDbParameter("#EventAdd", EventAdd),
new OleDbParameter("#WardNo", WardNo),
new OleDbParameter("#Program", Program),
new OleDbParameter("#NumAtt", NumAtt),
new OleDbParameter("#StTime", StTime),
new OleDbParameter("#EndTime", EndTime),
new OleDbParameter("#MngName", MngName),
new OleDbParameter("#RecKeeper", RecKeeper)
});
cmd.ExecuteNonQuery();
}
}
catch (Exception ex) { throw ex; }
finally
{
conn.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);
TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();
executeInsert(rbEventType.SelectedItem.Text, ddVolType.SelectedItem.Text,
txtDept.Text, txtEventDate.Text, meetDuration, txtEventName.Text,
txtEventAdd.Text, ddWard.SelectedItem.Value, txtSBPlan.Text, txtNumVol.Text,
txtStTime.Text, txtEndTime.Text, txtEventMgr.Text, txtRecording.Text);
}
What a lesson to learn. Apparently an ORACLE parameter has to be presented as a "?"
So, my final code... that actually successfully inserts a record is:
public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}
private void executeInsert()
{
OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) ";
sql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try
{
var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);
TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();
conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = rbEventType.SelectedItem.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = ddVolType.SelectedItem.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtDept.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtEventDate.Text;
cmd.Parameters.Add("?", OleDbType.Numeric).Value = meetDuration;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventName.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventAdd.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = ddWard.SelectedItem.Value;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtSBPlan.Text;
cmd.Parameters.Add("?", OleDbType.Numeric).Value = txtNumVol.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtStTime.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtEndTime.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventMgr.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtRecording.Text;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex) { throw ex; }
finally
{
conn.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
executeInsert();
}