Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#5
hi
i have a project and need help.anybody knows what must i do?
i have a page that give a patentid to any patient for giving service that is unique.every patient has some service and some paper.i have 2 table paper and givservice in sql.i want enter services that have the same patientid on giveservice table how must i do that?
here is my code:
 
private void insert_Click(object sender, EventArgs e)
 
        {
 

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=.;database=database;integrated security=true;";
            SqlCommand com1 = new SqlCommand();
            SqlCommand com2 = new SqlCommand();
            SqlCommand com3 = new SqlCommand();
            SqlCommand com4 = new SqlCommand();
            SqlCommand com5=new SqlCommand();
            com1.CommandText="DECLARE @papercode smallint";
            com2.CommandText = " insert into paper(date,drcode,patientid,unitid,personid)values(@date,@drcode,@patientid,@unitid,'15')";
            com5.CommandText = "SET @papercode=SCOPE_IDENTITY()";
            com3.CommandText = " Select @papercode=papercode From patient,Paper Where patient.patientid=Paper.patientid";
            com4.CommandText = "insert into givservice (papercode,price,tedad,khid) values ( @papercode,@price,@tedad,@khid)";
            com1.Connection = con;
            com2.Connection = con;
            com3.Connection = con;
             com4.Connection = con;
            com1.Parameters.AddWithValue("@date", txtdate.Text);
            com1.Parameters.AddWithValue("@patientid",txtpaziresh.Text);
             com1.Parameters.AddWithValue("@unitid",cmdunitname.SelectedValue);
             com1.Parameters.AddWithValue("@drcode", cmddrname.SelectedValue);
            com4.Parameters.AddWithValue("@tedad", txttedad.Text);
            com4.Parameters.AddWithValue("@price", cmdprice.SelectedValue);
            com4.Parameters.AddWithValue("@khid", cmdkh.SelectedValue);
            SqlParameter idOut = com5.Parameters.Add("@papercode", SqlDbType.SmallInt);
            idOut.Direction = ParameterDirection.Output;
            con.Open();
            com1.ExecuteNonQuery();
            SqlDataReader dr= com3.ExecuteReader();                  
            con.Close();
}
Posted 10-Oct-11 10:09am
Edited 11-Oct-11 22:28pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What version of SQL yoiu are using. If its 2005 or above then you can use insert statement with output clause.
Following link gives you detail about the same.
 
http://technet.microsoft.com/en-us/library/ms177564.aspx[^]
 
Hope it helps.
  Permalink  
Comments
sara2020 at 12-Oct-11 3:30am
   
i work sqlserver 2008r2 but i dont recieve my reply .please help more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 12 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100