Click here to Skip to main content
15,892,072 members
Home / Discussions / C#
   

C#

 
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

AnswerRe: unable to insert data Pin
Pete O'Hanlon4-Jul-13 21:58
mvePete O'Hanlon4-Jul-13 21:58 
GeneralRe: unable to insert data Pin
new to c# programming4-Jul-13 22:48
new to c# programming4-Jul-13 22:48 
AnswerRe: unable to insert data Pin
Simon_Whale4-Jul-13 22:02
Simon_Whale4-Jul-13 22:02 
GeneralRe: unable to insert data Pin
new to c# programming4-Jul-13 22:49
new to c# programming4-Jul-13 22:49 
QuestionC# shuffle one column out of two Pin
yandroide4-Jul-13 10:14
yandroide4-Jul-13 10:14 
AnswerRe: C# shuffle one column out of two Pin
Eddy Vluggen4-Jul-13 10:21
professionalEddy Vluggen4-Jul-13 10:21 
AnswerRe: C# shuffle one column out of two Pin
thanh_bkhn4-Jul-13 19:03
professionalthanh_bkhn4-Jul-13 19:03 
QuestionSplit string in multiple strings Pin
Sachin k Rajput 4-Jul-13 1:46
Sachin k Rajput 4-Jul-13 1:46 
AnswerRe: Split string in multiple strings PinPopular
Richard Deeming4-Jul-13 2:48
mveRichard Deeming4-Jul-13 2:48 
QuestionHow to use “Client Application Services” in Windows Forms Applications even in offline / disconnected mode Pin
devenv.exe3-Jul-13 23:21
professionaldevenv.exe3-Jul-13 23:21 
AnswerRe: How to use “Client Application Services” in Windows Forms Applications even in offline / disconnected mode Pin
Eddy Vluggen4-Jul-13 0:28
professionalEddy Vluggen4-Jul-13 0:28 
GeneralRe: How to use “Client Application Services” in Windows Forms Applications even in offline / disconnected mode Pin
devenv.exe4-Jul-13 1:10
professionaldevenv.exe4-Jul-13 1:10 
GeneralRe: How to use “Client Application Services” in Windows Forms Applications even in offline / disconnected mode Pin
Eddy Vluggen4-Jul-13 2:48
professionalEddy Vluggen4-Jul-13 2:48 
Questionexecute query Pin
new to c# programming3-Jul-13 20:25
new to c# programming3-Jul-13 20:25 
AnswerRe: execute query Pin
Amol_B3-Jul-13 20:46
professionalAmol_B3-Jul-13 20:46 
GeneralRe: execute query Pin
new to c# programming3-Jul-13 22:57
new to c# programming3-Jul-13 22:57 
GeneralRe: execute query Pin
new to c# programming4-Jul-13 0:21
new to c# programming4-Jul-13 0:21 

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.