PDA

View Full Version : مشکل در ثبت اطلاعات در دیتا گرید با یک تابع



Collector
چهارشنبه 30 شهریور 1390, 01:18 صبح
من دریک فرم که دو تا دیتا گرید دارد با این کد در یک کلاس اطلاعات را اضافه میکنم .
حالا در دیتا گرید اول اطلاعات را وارد میکنم و بعد در دیتاگرید دوم . دیتاگرید اولی با دومی یکی میشود.

آیا باید از دیتاست جدا استفاده کنم؟ لطفا راهنمایی کنید.

public static void Save(string txt,string Table,string Field)
{
string p;
Int32 i;

SqlDataAdapter da = new SqlDataAdapter("", con);
if (txt != "")
{
ds2.Clear();
da.SelectCommand.CommandText = "select code from " + Table + "";
da.Fill(ds2, "n");

i = End_Record(Table);

p = "insert into " + Table + "(" + Field + ",code) values ('" + txt + "'," + i + " )";
SqlCommand cmd = new SqlCommand(p, con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
da.SelectCommand.CommandText = "select * from " + Table + " order by code";
da.Fill(ds2, "B");
MessageBox.Show("." + "اطلاعات با موفقیت ثبت شد", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
}
else
{
MessageBox.Show("." + "لطفا نوع سقف مورد نظر را وارد کنید", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
}
}


فراخوانی تابع


Class1.Save(txt2.Text, "A", "name_A");
dgv1.DataSource = Class1.ds2;
dgv1.DataMember = "B";

Collector
جمعه 01 مهر 1390, 13:45 عصر
مشکل رو با این تابع در کلاس حل کردم.

public static void Save(string txt1,string Table,string Field,DataSet DS1)
{
string p;
Int32 i;

SqlDataAdapter da = new SqlDataAdapter("", con);
if (txt1 != "")
{
DS1.Clear();
da.SelectCommand.CommandText = "select code from " + Table + "";
da.Fill(DS1, "n");

i = End_Record(Table);

p = "insert into " + Table + "(" + Field + ",code) values ('" + txt1 + "'," + i + " )";
SqlCommand cmd = new SqlCommand(p, con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
da.SelectCommand.CommandText = "select * from " + Table + " order by code";
da.Fill(DS1, "B");
MessageBox.Show("." + "اطلاعات با موفقیت ثبت شد", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
}
else
{
MessageBox.Show("." + "لطفا نوع سقف مورد نظر را وارد کنید", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
}
}

فراخوانی تابع

Modiriat.Save(txt2.Text, "Table", "name_Field", ds1001);
dgv1.DataSource = ds1001;
dgv1.DataMember = "B";
Data_Grid_Head_Width();
txt_Clear();