PDA

View Full Version : درج تکراری



z.r2010
چهارشنبه 08 مهر 1388, 10:35 صبح
میخوام مشخصات یه نفر را در یک جدول با دو شناسه
ثبت کنم فرض کنید یه نفر میتونه هم مشتری باشه هم خریدار یه فیلد
id-table(کلید خارجی)
دراین جدولم دارم که برای مشتری وخریدار متفاوته فیلد
id-personal
برای این ادم باید متفاوت باشه
این کد


switch (a)
{
case 1:
string n;
string s = "select id_personal from personality where (lname=N'" + textBox28.Text + "')";
try
{
n = Microsoft.ApplicationBlocks.Data.SqlHelper.Execute Scalar("Data Source=.;Initial Catalog=garner;Integrated Security=True", CommandType.Text, s).ToString();
MessageBox.Show("كاربر گرامي مشخصات فرد مورد نظر قبلا ثبت شده است.");
}
catch
{
string strsql3 = "insert into personality (fname,lname,address,mobile,tell,id_table,tarikh)v alues(@fname,@lname,@address,@mobile,@tell,@id_tab le,@tarikh)";
con = new SqlConnection(constring());
com3 = new SqlCommand(strsql3, con);
com3.Parameters.Add("@fname", SqlDbType.NVarChar);
com3.Parameters.Add("@lname", SqlDbType.NVarChar);
com3.Parameters.Add("@address", SqlDbType.NVarChar);
com3.Parameters.Add("@mobile", SqlDbType.Int);
com3.Parameters.Add("@tell", SqlDbType.Int);
com3.Parameters.Add("@id_table", SqlDbType.Int);
com3.Parameters.Add("@tarikh", SqlDbType.NVarChar);
com3.Parameters["@tarikh"].Value = label61.Text;
com3.Parameters["@id_table"].Value = 4;

com3.Parameters["@fname"].Value = textBox24.Text;
com3.Parameters["@lname"].Value = textBox28.Text;
com3.Parameters["@address"].Value = textBox25.Text;
com3.Parameters["@mobile"].Value = textBox26.Text;
com3.Parameters["@tell"].Value = textBox27.Text;
//string s = "select lname,fname from personality where id_table=4 ";


result = MessageBox.Show("ایا مایلید طرف حساب ورود کالا بااین مشخصات ثبت گردد؟", "درج مشخصات", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
switch (result)
{
case (DialogResult.OK):

if (con.State == System.Data.ConnectionState.Closed)

con.Open();
//comsearch = new SqlCommand(s, con);
//SqlDataReader reader;
//reader = comsearch.ExecuteReader();
//if (reader.Read())
// MessageBox.Show("این مشخصات در پایگاه داده موجود میباشد");
//else
com3.ExecuteNonQuery();


MessageBox.Show("مشخصات طرف حساب ورود کالا ثبت گردید", "درج مشخصات", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox24.Clear(); textBox25.Clear(); textBox27.Clear(); textBox28.Clear(); textBox26.Clear();
comboBox10.Items.RemoveAt(0); comboBox11.Items.RemoveAt(0); comboBox12.Items.RemoveAt(0);
break;
case (DialogResult.Cancel):
comboBox10.Items.RemoveAt(0); comboBox11.Items.RemoveAt(0); comboBox12.Items.RemoveAt(0);
comboBox10.Items.RemoveAt(0); comboBox11.Items.RemoveAt(0); comboBox12.Items.RemoveAt(0);
break;
}
}
break;
case 2:
string nn;
string ss = "select id_personal from personality where (lname=N'" + textBox28.Text + "')";
try
{
nn = Microsoft.ApplicationBlocks.Data.SqlHelper.Execute Scalar("Data Source=.;Initial Catalog=garner;Integrated Security=True", CommandType.Text, ss).ToString();
MessageBox.Show(" کاربر گرامی مشخصات فرد مورد نظر قبلا ثبت شده است");
}
catch
{
string strsql4 = "insert into personality (fname,lname,address,mobile,tell,id_table,tarikh)v alues(@fname,@lname,@address,@mobile,@tell,@id_tab le,@tarikh)";
con = new SqlConnection(constring());
com4 = new SqlCommand(strsql4, con);
com4.Parameters.Add("@fname", SqlDbType.NVarChar);
com4.Parameters.Add("@lname", SqlDbType.NVarChar);
com4.Parameters.Add("@address", SqlDbType.NVarChar);
com4.Parameters.Add("@mobile", SqlDbType.Int);
com4.Parameters.Add("@tell", SqlDbType.Int);
com4.Parameters.Add("@id_table", SqlDbType.Int);
com4.Parameters.Add("@tarikh", SqlDbType.NVarChar);
com4.Parameters["@tarikh"].Value = label61.Text;
com4.Parameters["@id_table"].Value = 6;

com4.Parameters["@fname"].Value = textBox24.Text;
com4.Parameters["@lname"].Value = textBox28.Text;
com4.Parameters["@address"].Value = textBox25.Text;
com4.Parameters["@mobile"].Value = textBox26.Text;
com4.Parameters["@tell"].Value = textBox27.Text;
result = MessageBox.Show("ایا مایلید طرف حساب خروج کالا بااین مشخصات ثبت گردد؟", "درج مشخصات", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
switch (result)
{
case (DialogResult.OK):


if (con.State == System.Data.ConnectionState.Closed)

con.Open();
com4.ExecuteNonQuery();
MessageBox.Show("مشخصات طرف حساب خروج کالا ثبت گردید", "درج مشخصات", MessageBoxButtons.OK, MessageBoxIcon.Information);
comboBox10.Items.RemoveAt(0); comboBox11.Items.RemoveAt(0); comboBox12.Items.RemoveAt(0);
textBox24.Clear(); textBox25.Clear(); textBox27.Clear(); textBox28.Clear(); textBox26.Clear();
break;
case (DialogResult.Cancel):
comboBox10.Items.RemoveAt(0); comboBox11.Items.RemoveAt(0); comboBox12.Items.RemoveAt(0);
textBox24.Clear(); textBox25.Clear(); textBox27.Clear(); textBox28.Clear(); textBox26.Clear();

break;
}
}




بار اول که برای ورود کالا مشخصات رو ثبت میکنه برای خروج پیام میده که تکراریه در صورتی
id-table
متفاوته

لطفا راهنمایی کنید