I have a gridview that contains records When inserting the records to database They are numbered consecutively (7,8,9) I want to search for the largest value in the database Then number all fields in gridview with the following number (7,7,7)
i use this code for (int i = 0; i < gradeaddorder.Rows.Count; i++)
{
OracleCommand cmd = new OracleCommand("insert into orders (order_no,inserter_no,date_e,pharms_no,ITEMS_NO,quantity,bonus_type) VALUES ((select max(order_no) from orders)+1,'" + lblman_no.Text + "', to_date('" + TextBox1.Text + "12:00:00','YYYY-MM-DD HH:MI:ss') ,'" + hdpharms_no.Value + "','" + gradeaddorder.Rows[i].Cells[4].Text + "','" + gradeaddorder.Rows[i].Cells[2].Text + "','" + gradeaddorder.Rows[i].Cells[1].Text + "')", connect);
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
cmd.ExecuteNonQuery();
connect.Close();
}
Response.Write("<script>alert('تمت عملة الحفظ');</script>");
and this all code C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;//لاضافة الجدول
using Oracle.ManagedDataAccess.Client;
using System.Web.Services;
using System.Configuration;
public partial class orders : System.Web.UI.Page
{
OracleConnection connect = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;");
//تعريف تايبل من اجل اضافه العينات للجريد فيو
DataTable order = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
/////////////////////////
//من اجل هل تم تحميل الصفحه مجددا ام لا لاضافه العيانات للجريد فيو
if (!Page.IsPostBack)
{
if (ViewState["records"] == null)
{
order.Columns.Add("item_no");
order.Columns.Add("item_name");
order.Columns.Add("qun");
order.Columns.Add("bonus");
ViewState["records"] = order;
}
}
///////////////////////////////////اذا كان الرابط يذهب على طول لصفحه غير صفحة الدخول فيجب ان يعود لصفحه تسجيل الدخول كيف هذا\
//سنكتب الكود التالي الذي يقول عند تحميل الصفحه لو كان المتغير الذي ياخر قيمة رقم المستخدم فارغ اي انه لم يقم بادخال رقم المستخدم افتح صفحة تسجيل الدخول كالتالي
if (!Page.IsPostBack)
{
if (Session["user_no"] == null)
{
Response.Redirect("login.aspx");
}
}
// طباعة اسم الستتخدم على ليبل 7 ورقم المستخدم في ليبل الرقم
if (Session["user_name"] != null)
{
Label2.Text = Session["user_name"].ToString();
}
if (Session["user_no"] != null)
{
lblman_no.Text = Session["user_no"].ToString();//طباعة رقم المستخدم في اليبل
}
///////////////////////////////////////
///////////////////////////////اضهار رقم واسم المدينة التي يعمل بها المندوب الذي سجل الدخول
try
{
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
//المدينة التي يعمل فيها المندوب الذي سجل الدخول
OracleDataAdapter adbter_comp_no = new OracleDataAdapter("SELECT ALL mandob_up.comp_NO, comp.comp_NAME FROM comp, mandob_up WHERE mandob_up.comp_NO = comp.comp_NO and(mandob_up.man_no = '" + lblman_no.Text + "')", connect);
DataTable tab_comp_no = new DataTable();
connect.Close();
adbter_comp_no.Fill(tab_comp_no);
ddcomp.DataSource = tab_comp_no;
ddcomp.DataTextField = "comp_NAME";
ddcomp.DataValueField = "comp_NO";
ddcomp.DataBind();
///////////////////////اخذ رقم الشركة من اجل استخدامها في الويب ميثود
//Session["city_no"] = DropDownList4.SelectedValue;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
/////////////////////
///////////////////////////////// الطوة الثانية لعمل اوتو كمبليت حيث الاولى هي المفاتيح والتكست بوكس1 وبداخله اوتوكمبليت
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCompletionList(string prefixText)
{
using (OracleConnection con = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;"))
{
using (OracleCommand com = new OracleCommand())
{
com.CommandText = "select pharms_no, pharms_name from pharms where city_no= " + HttpContext.Current.Session["city_no"].ToString() + " and pharms_name like '%" + prefixText + "%' ";
com.Parameters.Add("TextBox2", prefixText);
com.Connection = con;
con.Open();
List<string> summ = new List<string>();
using (OracleDataReader sdr = com.ExecuteReader())
{
while (sdr.Read())
{
summ.Add(string.Format("{0}-{1}", sdr["pharms_NAME"], sdr["pharms_NO"]));//اذا اضفنا عمود جملة السكلته سنضيفه هنا كالبقية
}
}
con.Close();
return summ;
}
}
}
protected void btnback_Click(object sender, EventArgs e)
{
Response.Redirect("main.aspx");
}
protected void btnnew_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString("yyyy/MM/dd");
TextBox2.Enabled = true;
DropDownList2.Enabled = true;
DropDownList3.Enabled = true;
txt_qun.Enabled = true;
btnadd.Enabled = true;
btnsave.Enabled = true;
//عرض اسماء الاصناف الخاص بالشكره للنسحل الذي سجل الدخول
//OracleConnection connect = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;");
try
{
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
OracleDataAdapter adbter_items = new OracleDataAdapter("SELECT ALL items.ITEMS_NO, ITEMS.ITEMS_NAME FROM ITEMS WHERE (items.comp_no = '" + ddcomp.SelectedValue + "')", connect);
DataTable tab_items = new DataTable();
connect.Close();
adbter_items.Fill(tab_items);
DropDownList2.DataSource = tab_items;
DropDownList2.DataTextField = "ITEMS_NAME";
DropDownList2.DataValueField = "ITEMS_NO";
DropDownList2.DataBind();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
///////////////////////////////اضهار رقم واسم المدينة التي يعمل بها من سجل الدخول
try
{
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
//المدينة التي يعمل فيها المندوب الذي سجل الدخول
OracleDataAdapter adbter_city_no = new OracleDataAdapter("SELECT ALL mandob_up.city_NO, city.city_NAME FROM city, mandob_up WHERE mandob_up.city_NO = city.city_NO and(mandob_up.man_no = '" + lblman_no.Text + "')", connect);
DataTable tab_city_no = new DataTable();
connect.Close();
adbter_city_no.Fill(tab_city_no);
ddcity.DataSource = tab_city_no;
ddcity.DataTextField = "CITY_NAME";
ddcity.DataValueField = "CITY_NO";
ddcity.DataBind();
///////////////////////اخذ رقم المدينه من اجل استخدامها في الويب ميثود
//اذا كان الدروب داون الخاص بالمدينه فارغ فانه سيعمل لنا مشكله عند تسجيل الطلبيات
//لانه في حالة دخل للصفحه شخص غير منتسب لمدينه فانه عند اختيار صيدليه ستضهر الايرور
//لذلك هنا سنعمل شرط بحالة ان الدروب داون فارغ فانه يظهر رسله للمتخدم ويرجع للشاشه الرئيسية
if (string.IsNullOrEmpty(ddcity.Text))
{
string message = "انت غير منتسب لمدينة. راجع مسئول النظام";
string url = "main.aspx"; //للذهاب لصفحه في الموقع
//string url = "//google.com/"; //للذهاب موقع محدد
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
else
{
//اخذ رقم المدينه من اجل استخدامها في الويب ميثود
Session["city_no"] = ddcity.SelectedValue;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
if(hdpharms_no.Value=="")//اولا التحقق من ان اسم العميل موجود
{
Response.Write("<script>alert('ادخل اسم العميل');</script>");
}
else
{
if ( txt_qun.Text=="")//التحقق من ادخل كمية الطلبيه للنصف الذي سيتم انزاله للجريدفيو
{
Response.Write("<script>alert('ادخل كمية الطلبية');</script>");
}
else
{
//جملة الشرط التالية تقول لوكان حقل كمية العينات اقل او تساوي صفر اضهر الليبل الذي يقول دخل قيمة اكبر من صفر
if (int.Parse(txt_qun.Text) < 1)
{
Label7.Text = "ادخل قيمة اكبر من صفر"; //تم استخدام انتجر دوت بارس لانه لانه يقبل فقط سترينج داخل التكست بوكس
}
else
{
//اضافه للجريد فيو الخاصه بالعينات
order = (DataTable)ViewState["records"];
order.Rows.Add(DropDownList2.SelectedItem.Value, DropDownList2.SelectedItem.Text, txt_qun.Text, DropDownList3.SelectedValue);
gradeaddorder.DataSource = order;
gradeaddorder.DataBind();
}
DropDownList2.SelectedIndex = 0;
DropDownList3.SelectedIndex = 0;
txt_qun.Text = "";
}
}
}
protected void gradeaddorder_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//لحذف صف من الجريد فيو عند الضغط على علامة الاكس
order = (DataTable)ViewState["records"];
order.Rows[e.RowIndex].Delete();
gradeaddorder.DataSource = order;
gradeaddorder.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
try
{
if (hdpharms_no.Value == "")//اولا التحقق من ان اسم العميل موجود
{
Response.Write("<script>alert('ادخل اسم العميل');</script>");
}
else
{
for (int i = 0; i < gradeaddorder.Rows.Count; i++)
{
OracleCommand cmd = new OracleCommand("insert into orders (order_no,inserter_no,date_e,pharms_no,ITEMS_NO,quantity,bonus_type) VALUES ((select max(order_no) from orders)+1,'" + lblman_no.Text + "', to_date('" + TextBox1.Text + "12:00:00','YYYY-MM-DD HH:MI:ss') ,'" + hdpharms_no.Value + "','" + gradeaddorder.Rows[i].Cells[4].Text + "','" + gradeaddorder.Rows[i].Cells[2].Text + "','" + gradeaddorder.Rows[i].Cells[1].Text + "')", connect);
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
cmd.ExecuteNonQuery();
connect.Close();
}
Response.Write("<script>alert('تمت عملة الحفظ');</script>");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
//تفريغ الحقول عند الحفظ
TextBox2.Text = "";
txt_qun.Text = "";
DropDownList2.SelectedIndex = 0;
DropDownList3.SelectedIndex = 0;
gradeaddorder.DataSource = null;
gradeaddorder.DataBind();
//تفريغ الداتا تايبل
DataTable order = (DataTable)ViewState["records"];
order.Rows.Clear();
ViewState["records"] = order;
//الغاء تفعيل الازرار بعد عملية الحفظ من اجل عملية جديده
TextBox2.Enabled = false;
DropDownList2.Enabled = false;
DropDownList3.Enabled = false;
txt_qun.Enabled = false;
btnadd.Enabled = false;
btnsave.Enabled = false;
}
}
Before saving, it brings the largest value to the dropdown list
After that, I get this value and included it in the insert statement
but
Is this way fast?
If more than one user saves at the same moment
If you need to insert multiple records into the database at the same time and keep the same number, you really need to save the number (using hidden field controls,Cookie or other storage methods), and then execute the corresponding insert statement.
On the other hand, if you want to control the number to not cause problems when multiple users operate, you may need to use Thread Synchronization related concepts.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
9 Points
39 Posts
Numbering all records in the grid view with a unified number
Sep 19, 2020 05:04 PM|alhakimy|LINK
Member
9 Points
39 Posts
Re: Numbering all records in the grid view with a unified number
Sep 20, 2020 04:49 PM|alhakimy|LINK
Contributor
2070 Points
659 Posts
Re: Numbering all records in the grid view with a unified number
Sep 21, 2020 09:45 AM|XuDong Peng|LINK
Hi alhakimy,
If you need to insert multiple records into the database at the same time and keep the same number, you really need to save the number (using hidden field controls,Cookie or other storage methods), and then execute the corresponding insert statement.
On the other hand, if you want to control the number to not cause problems when multiple users operate, you may need to use
Thread Synchronization related concepts.
Best regards,
Xudong Peng