|
The clues are actually in the names. As you have probably guessed, ExecuteNonQuery is used for executing something that is not a query, i.e. an INSERT , UPDATE or DELETE . ExecuteScalar is used to return a scalar value from a query - a scalar is simply a single value. As a scalar is a single value, it requires less code than issuing something like ExecuteReader because the value is available for you to use immediately.
|
|
|
|
|
I just realised why my DAL only uses GetTable for 90% of our work, I get the inserted/updated record returned, as almost all my tables have a view this is the simplest way of getting all the related info. Delete and processing procs are the only exception and they return ints
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
thanks for the help
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
resetLabels();
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\patientdb.mdf;Integrated Security=True;User Instance=True";
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "select convert(varchar(50),getdate(),105) as date";
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
date = sdr["date"].ToString();
}
sdr.Close();
setPatientidtxt();
if (updateflag)
{
cmd.CommandText = "update Patient set" +
" name=@FirstName,surname=@LastName,age=@Age,city=@City,gender=@Gender,allergy=@Allergy,phoneno=@ContactNo,consultingfees=@Fees,note=@Note,date=@Date,time=@Time" +
" where patientid=@Patientid";
cmd.Parameters.AddWithValue("@patientid",textBox1.Text);
}
else
{
cmd.CommandText = "insert into patient values" +
"(@FirstName,@LastName,@Age,@City,@Gender,@Allergy,@ContactNo,@Fees,@Note,@Date,@Time)";
}
cmd.Parameters.AddWithValue("@FirstName", textBox2.Text);
cmd.Parameters.AddWithValue("@LastName", textBox3.Text);
cmd.Parameters.AddWithValue("@Age", textBox4.Text);
cmd.Parameters.AddWithValue("@City", textBox5.Text);
//radio button
if (radioButton1.Checked == true)
{
cmd.Parameters.AddWithValue("@Gender", radioButton1.Text);
}
else
{
cmd.Parameters.AddWithValue("@Gender", radioButton2.Text);
}
if (radioButton3.Checked == true)
{
cmd.Parameters.AddWithValue("@Allergy", radioButton3.Text);
}
else
{
cmd.Parameters.AddWithValue("@Allergy", radioButton4.Text);
}
cmd.Parameters.AddWithValue("@ContactNo", textBox6.Text);
cmd.Parameters.AddWithValue("@Fees", textBox7.Text);
cmd.Parameters.AddWithValue("@Note", textBox8.Text);
cmd.Parameters.AddWithValue("@Date", System.DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@Time", System.DateTime.Now.ToLongTimeString());
// here the exception error (break) is comming at ExecuteNonQuery)
cmd.ExecuteNonQuery();
button4.Enabled = true;
label11.Visible = true;
label4.Visible = true;
timer1.Start();
}
}
I am getting an error as :
"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Column name or number of supplied values does not match table definition."
and sequence of columns in my table is (patientid,name,surname,age,city,gender,allergy,phoneno,consultingfees,note,date,time)
i am wondering where is sequence wrong in my code. .
|
|
|
|
|
|
Hi,
I am dynamically adding text to pdf file using ColumnText.ShowTextAligned method. I want to show dynamic text vertically top left. Is there any solution?
|
|
|
|
|
it might others help you, if you showed the code and explained what SDK/toolkit you were using to perform this task
'g'
|
|
|
|
|
Hi,
Below is the code i am using.
using (FileStream stream1 = File.Open(DestinationPath + "\\" + Path.GetFileName(SourceFilePath), FileMode.Open))
{
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
//reader = new PdfReader(DestinationPath + "\\" + Path.GetFileName(SourceFilePath));
var pageSize = reader.GetPageSize(1);
PdfStamper stamper = new PdfStamper(reader, stream1);
iTextSharp.text.Font tmpFont = new iTextSharp.text.Font(bf, 9);
Phrase ph = new Phrase();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfContentByte canvas = stamper.GetOverContent(i);
ph = new Phrase(NewBatesStatmpingText, tmpFont);
ph.Font = tmpFont;
canvas.SetFontAndSize(bf, 9);
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, ph, SCX, SCY, 0);
}
|
|
|
|
|
When the query is work will call an insert query but when it is fail will appear a message that the record didn't founded in database
|
|
|
|
|
And what is your question? I can't see one in there at all.
|
|
|
|
|
when the query is false how can i take the result so i can use it for doing something else with this
|
|
|
|
|
What query? How is it returning false? You haven't shown a query.
|
|
|
|
|
OleDbConnection dbConnDest;
dbConnDest = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= SystemA.accdb");
string query_select = "SELECT ReferenceYear FROM [MS Access;DATABASE=" + dialog.FileName +"].Questionnaires_Table1 Where ReferenceYear = '" + dateref.Text + "';";
OleDbCommand cmd_year = new OleDbCommand(query_select, dbConnDest);
dbConnDest.Open();
string rfyear = cmd_year.ExecuteScalar().ToString();
|
|
|
|
|
So all you're doing is testing to see if ReferenceYear exists? Why not just do SELECT Count(1)... instead, and then just evaluate the count? If it's zero, the record doesn't exist? Also, you may want to pay attention to the fact your select statement is vulnerable to a SQL Injection attack.
|
|
|
|
|
Exactly true..
KiranKumar Roy
|
|
|
|
|
ok i make a class and i have there the following code:
public DataTable findrefyear(string dateref)
{
try
{
//Set the access command string
string query_insert = "INSERT INTO Questionnaires_Table (BranchName,Factor,Region,Branch_ID,[Current_Date],No_Employees,"
+ "No_Cashdesk,UrbanArea,TouristArea,RuralArea,GreenLinearArea,CentralStreets,EscapeBack,Collonization,"
+ "TimeDelay,PatrolPolice,Visibility,SafeBoxRisk,StaffAccess,KeyboardActiviation,ConnectionKSC,GSMCommunicator,SystemAlarmRisk,"
+ "InstalledSKKT,CheckSKKT,CheckDesk,CheckEntrance,SKKTRisk,RenovatedStore,MetalCashDrawers,ExtensionCash,Limits,"
+ "CashDrawers,CasherRisk,WarningAlert,SecurityWarningDelay,WarningRisk,BothSideDoors,"
+ "DoorsSound,ExitRisk,EmergencyCase,RobberyCase,DescriptionForm,SeminarRobbery,EducationRisk,ReferenceYear) "
+ "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees,"
+ "No_Cashdesk,UrbanArea,TouristArea,RuralArea,GreenLinearArea,CentralStreets,EscapeBack,Collonization,"
+ "TimeDelay,PatrolPolice,Visibility,(TimeDelay+PatrolPolice+Visibility)/3,StaffAccess,KeyboardActiviation,ConnectionKSC,GSMCommunicator,(StaffAccess+KeyboardActiviation+ConnectionKSC+GSMCommunicator)/4,"
+ "InstalledSKKT,CheckSKKT,CheckDesk,CheckEntrance,(InstalledSKKT+CheckSKKT)/2,RenovatedStore,MetalCashDrawers,ExtensionCash,Limits,"
+ "CashDrawers,(CheckDesk+CheckEntrance+RenovatedStore+MetalCashDrawers+ExtensionCash+Limits+CashDrawers)/7,"
+ "WarningAlert,SecurityWarningDelay,(WarningAlert+SecurityWarningDelay)/2,BothSideDoors,"
+ "DoorsSound,(BothSideDoors+DoorsSound)/2,"
+ "EmergencyCase,RobberyCase,DescriptionForm,SeminarRobbery,(EmergencyCase+RobberyCase+DescriptionForm+SeminarRobbery)/4,ReferenceYear "
+ "FROM [MS Access;DATABASE=" + dialog.FileName + "].Questionnaires_Table1 Where ReferenceYear = '" +dateref + "';";
//create new new access connection to the database
OleDbConnection conn = new OleDbConnection(ConnectionString);
//create new access data adapter
OleDbDataAdapter oleDBAdapter = new OleDbDataAdapter(query_insert, conn);
//create new access command builder
OleDbCommandBuilder oleDBBuilder = new OleDbCommandBuilder(oleDBAdapter);
dTable = new DataTable();
oleDBAdapter.Fill(dTable);
conn.Close();
}
catch (OleDbException e)
{
MessageBox.Show(e.ToString());
}
//return the search results on a datatable
return dTable;
}
Also i have in appropriate form the following code that call the query when it is work:
dbConnDest.Open();
DataTable find_data = importrefyear.findrefyear(dateref.Text);
if (find_data.Rows.Count >= 1)
{
"How i CAll the query from here???"
}
Also the SELECT Count(1)is the above method i use ??
|
|
|
|
|
What is the result of a query in c# when is false or is true
for example i have the following query and i want to do something when is false. How i manage this?
string rfyear = ((string)cmd_year.ExecuteScalar());
i want to manage with "if" if i can to do something like this...
if (rfyear.Length == 0) { MessageBox.Show("the query is false "); }
else{ MessageBox.Show("OK");
textBox1.Text = rfyear;
|
|
|
|
|
It all depends on what your query is actually returning: it is very possible that your line:
string rfyear = ((string)cmd_year.ExecuteScalar()); Will not work, but throw an InvalidCastException because the data coming back can't be cast to a string - it may work if you change it to:
string rfyear = cmd_year.ExecuteScalar().ToString(); But...I wouldn't recommend it as there is a very good chance that you will get a string containing the name of the type of the value returned.
What does your query return when it works and when it fails?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
I just came to know about the Linux tracing tool mechanism available in Java (LTTng - Linux Tracing Toolkit Next Generation).
It actually is a framework that provides analysis of traces and events on a linux machine.
I want to develop a windows application which will read linux traces(usually in CTF format - common Trace Format) from a file and decode the value and time of trace. This application runs on Windows PC.
Can any body suggest me the best possible approach for this?
|
|
|
|
|
If I were looking to do this, I'd first of all look to see if anyone had already written code that did this; if they hadn't, I'd then look at the specifications[^].
|
|
|
|
|
I've made an application that works with SQL database. Now want without install the software SQL on client computer ,an applicationto work.
what way do you suggest?
|
|
|
|
|
Use a different database, like SQLite. SQL Server must be on the client computer, or the client computer must be able to connect to the SQL server remotely, it doesn't run without the back-end installed on something that it can talk to.
|
|
|
|
|
Ron Beyer wrote: or the client computer must be able to connect to the SQL server remotely, it doesn't run without the back-end installed on something that it can talk to.
Thanks for the Answer
Can you explain more about connecting remotely, step by step
Thanks...
|
|
|
|
|
|
I think you are not talking about web application. In web application DB will setup in SQL server. For a desktop application sql server is not a recommended DB. You have setup SQL server in client machine also for desktop app
|
|
|
|
|