Click here to Skip to main content
15,922,894 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# error Pin
Richard MacCutchan5-Jul-13 22:28
mveRichard MacCutchan5-Jul-13 22:28 
GeneralRe: c# error Pin
User34906-Jul-13 10:25
User34906-Jul-13 10:25 
GeneralRe: c# error Pin
NotPolitcallyCorrect6-Jul-13 10:35
NotPolitcallyCorrect6-Jul-13 10:35 
GeneralRe: c# error Pin
Eddy Vluggen6-Jul-13 11:35
professionalEddy Vluggen6-Jul-13 11:35 
GeneralRe: c# error Pin
User34906-Jul-13 12:15
User34906-Jul-13 12:15 
GeneralRe: c# error Pin
Eddy Vluggen9-Jul-13 8:55
professionalEddy Vluggen9-Jul-13 8:55 
GeneralRe: c# error Pin
Richard MacCutchan6-Jul-13 21:42
mveRichard MacCutchan6-Jul-13 21:42 
GeneralRe: c# error Pin
User34907-Jul-13 4:26
User34907-Jul-13 4:26 
GeneralRe: c# error Pin
Richard MacCutchan7-Jul-13 7:21
mveRichard MacCutchan7-Jul-13 7:21 
GeneralRe: c# error Pin
User34907-Jul-13 8:28
User34907-Jul-13 8:28 
GeneralRe: c# error Pin
Richard MacCutchan7-Jul-13 20:38
mveRichard MacCutchan7-Jul-13 20:38 
QuestionC# SQL SELECT query taking too long Pin
IceUK5-Jul-13 5:48
IceUK5-Jul-13 5:48 
AnswerRe: C# SQL SELECT query taking too long Pin
fixthebugg5-Jul-13 6:00
fixthebugg5-Jul-13 6:00 
AnswerRe: C# SQL SELECT query taking too long Pin
Simon_Whale5-Jul-13 6:14
Simon_Whale5-Jul-13 6:14 
AnswerRe: C# SQL SELECT query taking too long Pin
Mycroft Holmes5-Jul-13 14:03
professionalMycroft Holmes5-Jul-13 14:03 
QuestionI am geting an error about callbackOnCollectedDelegate Pin
mahua225-Jul-13 2:11
mahua225-Jul-13 2:11 
AnswerRe: I am geting an error about callbackOnCollectedDelegate Pin
Ron Beyer5-Jul-13 3:40
professionalRon Beyer5-Jul-13 3:40 
AnswerRe: I am geting an error about callbackOnCollectedDelegate Pin
Alan N5-Jul-13 3:48
Alan N5-Jul-13 3:48 
GeneralRe: I am geting an error about callbackOnCollectedDelegate Pin
Ron Beyer5-Jul-13 4:44
professionalRon Beyer5-Jul-13 4:44 
QuestionDifference between {list}.First() and {list}[0] Pin
velvet75-Jul-13 0:59
velvet75-Jul-13 0:59 
AnswerRe: Difference between {list}.First() and {list}[0] Pin
Marco Bertschi5-Jul-13 1:18
protectorMarco Bertschi5-Jul-13 1:18 
AnswerRe: Difference between {list}.First() and {list}[0] Pin
Nicholas Marty5-Jul-13 1:24
professionalNicholas Marty5-Jul-13 1:24 
AnswerRe: Difference between {list}.First() and {list}[0] Pin
Pete O'Hanlon5-Jul-13 3:19
mvePete O'Hanlon5-Jul-13 3:19 
AnswerRe: Difference between {list}.First() and {list}[0] Pin
Abhinav S6-Jul-13 1:41
Abhinav S6-Jul-13 1:41 
Questionunable to insert data Pin
new to c# programming4-Jul-13 21:49
new to c# programming4-Jul-13 21:49 
HI ,

XML
even after inserting different patient id on windows form and when clicked save button.
I am getting an Error :
<pre lang="css">An unhandled exception of type &#39;System.Data.SqlClient.SqlException&#39; occurred in System.Data.dll

Additional information: Violation of PRIMARY KEY constraint &#39;pk_patienid&#39;. Cannot insert duplicate key in object &#39;dbo.patient&#39;.
The statement has been terminated.</pre>


even after inserting different patient id on windows form and when clicked save button.


my code is :
private void button1_Click(object sender, EventArgs e)
{
resetLabels();


using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=Q:\Dotnet\dental1\dental1\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", Convert.ToInt64(textBox1.Text));
}
else
{
cmd.CommandText = "insert into patient (patientid,name,surname,age,city,gender,allergy,phoneno,consultingfees,note,date,time) values" +
"(@patientid,@FirstName,@LastName,@Age,@City,@Gender,@Allergy,@ContactNo,@Fees,@Note,@Date,@Time)";
}
cmd.Parameters.AddWithValue("@patientid", textBox1.Text);
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());
cmd.ExecuteNonQuery();

button4.Enabled = true;
label11.Visible = true;
label4.Visible = true;

timer1.Start();


}

}

only first time data is saved in database: and ome colums as 0
SQL
patientid	name	surname	age	city	gender	allergy	phone no 	consultationfes		date	time
0			0		        Male      No		0		0               07-05-2013	 00:23:06
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.