|
 |
|
ต้องการทำเลขลำดับแถว เริ่มจาก 1 ถึง บรรทัดสุดท้ายไม่ได้ครับ รบกวนแนะนำด้วยครับ |
|
 |
|
|
 |
 |
|
คือผมอยากให้ลำดับของ LEVEL_SEQ เริ่มจาก 1,2,3,4,5,6,7,...ไปจนถึงลำดับเลขบรรทัดสุดท้ายครับ พยายามลองดูก็ยังไม่ได้ รบกวนแนะนำหน่อยนะครับ หัวผมตื้อคิดมะออกครับ ขอบคุณครับ 

Code (C#)
protected void Button3_Click(object sender, EventArgs e)
{
RadioButton RG1 = null;
if (RadioButton1.Checked == true)
{
RG1 = RadioButton1;
SqlTransaction tr = objConn.BeginTransaction();
SqlCommand cmd = objConn.CreateCommand();
try
{
cmd.CommandType = CommandType.Text;
cmd.Transaction = tr;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string typeCarbinet = "1";
string typeDrawer = "2";
string typeFiles = "3";
string typeBinder = "4";
string chkCabinet = GridView1.Rows[i].Cells[5].Text.Trim();
string chkDrawer = GridView1.Rows[i].Cells[6].Text.Trim();
string chkFiles = GridView1.Rows[i].Cells[7].Text.Trim();
string chkBinder = GridView1.Rows[i].Cells[8].Text.Trim();
int levelseq = 1;
if (i >= 0)
{
bool testchk = false;
for (int c = 0; c < i; c++) //###Check Cabinet###
if (GridView1.Rows[i].Cells[5].Text.Trim() == GridView1.Rows[c].Cells[5].Text.Trim())
{
testchk = true;
}
if (testchk == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeCarbinet + "','" + chkCabinet + "')";
cmd.ExecuteNonQuery();
levelseq++;
}
}
if (i >= 0)
{
bool testchk = false;
for (int d = 0; d < i; d++) //###Check Drawer###
if (GridView1.Rows[i].Cells[6].Text.Trim() == GridView1.Rows[d].Cells[6].Text.Trim())
{
testchk = true;
}
if (testchk == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeDrawer + "','" + chkDrawer + "')";
cmd.ExecuteNonQuery();
levelseq++;
}
}
if (i >= 0)
{
bool testchk = false;
for (int f = 0; f < i; f++) //###Check Files###
if (GridView1.Rows[i].Cells[7].Text.Trim() == GridView1.Rows[f].Cells[7].Text.Trim() && GridView1.Rows[i].Cells[6].Text.Trim() == GridView1.Rows[f].Cells[6].Text.Trim())
{
testchk = true;
}
if (testchk == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeFiles + "','" + chkFiles + "')";
cmd.ExecuteNonQuery();
levelseq++;
}
}
if (i >= 0)
{
bool testchk = false;
for (int b = 0; b < i; b++) //###Check Binder###
if (GridView1.Rows[i].Cells[8].Text.Trim() == GridView1.Rows.Cells[8].Text.Trim())
{
testchk = true;
}
if (testchk == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeBinder + "','" + chkBinder + "')";
cmd.ExecuteNonQuery();
levelseq++;
}
}
}
tr.Commit();
Label4.Text = "บันทึกข้อมูลเรียบร้อย";
}
catch (SqlException ex)
{
tr.Rollback();
Label2.Text = "Error - TRANSACTION ROLLED BACK\n" + ex.Message;
}
catch (Exception ex)
{
tr.Rollback();
Label2.Text = "System Error\n" + ex.Message;
}
finally
{
objConn.Close();
tr.Dispose();
}
}
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)

|
|
 |
 |
 |
 |
Date :
2012-12-25 14:28:58 |
By :
SponG_Gy |
View :
1292 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
int levelseq = i+1;
|
 |
 |
 |
 |
Date :
2012-12-25 15:46:52 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณนะครับ ผมก็นั่งคิด ลองไปลองมาหลายต่อหลายรอบ ที่แท้ตรงนี้นี่เอง 
ขอบคุณมากนะครับ คุณ kanchen
|
 |
 |
 |
 |
Date :
2012-12-25 16:06:28 |
By :
SponG_Gy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ kanchen ขอโทดนะครับ พอดีผมเพิ่งมาสังเกตุเห็น จากที่ คุณ kanchen ได้แนะนำผมให้นำ int levelseq = i+1;
ผมลองดูผลลัพธ์ ลำดับเลขเรียง 1,2,3,...9 พอ 10 กลายเป็นกระโดดไป 13 // พอ 18 ก็ซ้ำกัน 2 ครั้ง
พอ 19,20 แล้วก็ 19,20 อีกรอบ เป็นเพราะอะไรหรอครับ แนะนำทีครับ เพิ่งจะสังเกตุเห็นว่ายังเรียงผิดอยู่ครับ
รบกวนช่วยหน่อยนะครับ ขอบคุณครับ

โค๊ดที่ใช้เหมือนข้างบนเลยครับ ถ้าใครแนะนำได้ รบกวนด้วยนะครับ ขอบคุณมากครับ
|
ประวัติการแก้ไข 2013-01-02 11:15:48
 |
 |
 |
 |
Date :
2013-01-02 11:14:46 |
By :
SponG_Gy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
protected void Button3_Click(object sender, EventArgs e)
{
RadioButton RG1 = null;
if (RadioButton1.Checked == true)
{
RG1 = RadioButton1;
SqlTransaction tr = objConn.BeginTransaction();
SqlCommand cmd = objConn.CreateCommand();
try
{
cmd.CommandType = CommandType.Text;
cmd.Transaction = tr;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string typeCarbinet = "1";
string typeDrawer = "2";
string typeFiles = "3";
string typeBinder = "4";
string chkCabinet = GridView1.Rows[i].Cells[5].Text.Trim();
string chkDrawer = GridView1.Rows[i].Cells[6].Text.Trim();
string chkFiles = GridView1.Rows[i].Cells[7].Text.Trim();
string chkBinder = GridView1.Rows[i].Cells[8].Text.Trim();
int levelseq = 1;
if (i >= 0)
{
bool testchk1 = false;
for (int c = 0; c < i; c++) //###Check Cabinet###
{
if (GridView1.Rows[i].Cells[5].Text.Trim() == GridView1.Rows[c].Cells[5].Text.Trim())
{
testchk1 = true;
break;
}
}
if (testchk1 == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeCarbinet + "','" + chkCabinet + "')";
cmd.ExecuteNonQuery();
}
bool testchk2 = false;
for (int d = 0; d < i; d++) //###Check Drawer###
{
if (GridView1.Rows[i].Cells[6].Text.Trim() == GridView1.Rows[d].Cells[6].Text.Trim())
{
testchk2 = true;
break;
}
}
if (testchk2 == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeDrawer + "','" + chkDrawer + "')";
cmd.ExecuteNonQuery();
}
bool testchk3 = false;
for (int f = 0; f < i; f++) //###Check Files###
{
if (GridView1.Rows[i].Cells[7].Text.Trim() == GridView1.Rows[f].Cells[7].Text.Trim() && GridView1.Rows[i].Cells[6].Text.Trim() == GridView1.Rows[f].Cells[6].Text.Trim())
{
testchk3 = true;
break;
}
}
if (testchk3 == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeFiles + "','" + chkFiles + "')";
cmd.ExecuteNonQuery();
}
bool testchk4 = false;
for (int b = 0; b < i; b++) //###Check Binder###
{
if (GridView1.Rows[i].Cells[8].Text.Trim() == GridView1.Rows.Cells[8].Text.Trim())
{
testchk4 = true;
break;
}
}
if (testchk4 == false)
{
cmd.CommandText = "Insert Into DIP_LEVEL2(LEVEL_SEQ,LEVEL_TYPE,LEVEL_NAME)" + " values ('" + levelseq + "','" + typeBinder + "','" + chkBinder + "')";
cmd.ExecuteNonQuery();
}
levelseq = levelseq + 1;
}
}
tr.Commit();
Label4.Text = "บันทึกข้อมูลเรียบร้อย";
}
catch (SqlException ex)
{
tr.Rollback();
Label2.Text = "Error - TRANSACTION ROLLED BACK\n" + ex.Message;
}
catch (Exception ex)
{
tr.Rollback();
Label2.Text = "System Error\n" + ex.Message;
}
finally
{
objConn.Close();
tr.Dispose();
}
}
แบบนี้ได้ไหมครับ
ถ้าผิด ขออภัยด้วยครับ
|
ประวัติการแก้ไข 2013-01-02 15:27:06 2013-01-02 15:30:52
 |
 |
 |
 |
Date :
2013-01-02 15:25:14 |
By :
Niratiam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้ครับ คุณ Niratiam ผลที่ได้คือเลข 1 ทุกแถวเลยครับ
|
 |
 |
 |
 |
Date :
2013-01-02 15:49:38 |
By :
SponG_Gy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมแก้ได้แล้วครับ พอดีเอ๊ะใจ เลยวาง int levelseq = 1; ไว้นอก loop
แล้วเอา levelseq++; วางไว้ในแต่ละ if ที่เป็น false ยังไงก็ขอบคุณ คุณ "Niratiam" นะครับ เคยช่วยแนะนำผม
แล้วในครั้งนี้ก็มาแนะนำผมอีก ยังไงก็ขอบคุณมากนะครับ 
|
ประวัติการแก้ไข 2013-01-02 16:01:42
 |
 |
 |
 |
Date :
2013-01-02 16:01:03 |
By :
SponG_Gy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|