Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ทำ AUTO ID แล้วติด ERROR System.FormatException อ่ะครับ



 

ทำ AUTO ID แล้วติด ERROR System.FormatException อ่ะครับ

 



Topic : 105906



โพสกระทู้ ( 84 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



คือว่า ผมต้องการทำ AUTO ID โดยรหัสนำได้จาก Combobox ครับ
ผมทำแล้วติด error "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format."
ซึ่งมันมาร์กไว้ที่บรรทัด NewId = Convert.ToInt32(obj.ToString()) + 1; เป็นตอนเลือก Combobox อ่ะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace test01
{
    public partial class dataCus : Form
    {
        public dataCus()
        {
            InitializeComponent();
        }

        string taxrequire;
        private void dataCus_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mejiDataSet.customer_status' table. You can move, or remove it, as needed.
            this.customer_statusTableAdapter.Fill(this.mejiDataSet.customer_status);
            // TODO: This line of code loads data into the 'mejiDataSet.customer_group' table. You can move, or remove it, as needed.
            this.customer_groupTableAdapter.Fill(this.mejiDataSet.customer_group);
            
            SqlConnection myConnection = default(SqlConnection);
            connection NewConnection = new connection();
            NewConnection.Connection_Today();
            connection.myConnection.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM customer_group", connection.myConnection);
            da1.Fill(ds, "combolist");
            txtcustgroupcode.DataSource = ds;//กำหนด DataSet
            txtcustgroupcode.DisplayMember = "combolist.ecustgroupname";
            txtcustgroupcode.ValueMember = "combolist.custgroupcode";

            DataSet ds2 = new DataSet();
            SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM customer_status", connection.myConnection);
            da2.Fill(ds2, "combolist");
            txtcuststatuscode.DataSource = ds2;//กำหนด DataSet
            txtcuststatuscode.DisplayMember = "combolist.ecuststatusname";
            txtcuststatuscode.ValueMember = "combolist.custstatuscode";

            connection.myConnection.Close();
        }

        public string newid()
        {
            string AtCnt;
            SqlConnection myConnection = default(SqlConnection);
            connection NewConnection = new connection();
            NewConnection.Connection_Today();
            connection.myConnection.Open();

            SqlCommand myCommand = connection.myConnection.CreateCommand();
            myCommand.CommandText = "SELECT MAX(substring(custcode,2,9)) FROM customer WHERE custgroupcode = '" + txtcustgroupcode.SelectedValue.ToString() + "' ";
            myCommand.CommandType = CommandType.Text;
            AtCnt = Convert.ToString(myCommand.ExecuteScalar());
            connection.myConnection.Close();
            return AtCnt;
        }

        private void txttaxrequired1_CheckedChanged(object sender, EventArgs e)
        {
            taxrequire = "y";
        }

        private void txttaxrequired2_CheckedChanged(object sender, EventArgs e)
        {
            taxrequire = "n";
        }


        private void bntsubmit_Click(object sender, EventArgs e)
        {
            string bow = "aphinya";

            // connect database from class connection
            SqlConnection myConnection = default(SqlConnection);
            connection NewConnection = new connection();
            NewConnection.Connection_Today();
            connection.myConnection.Open();

            // command for select/insert/update/dalete
            SqlCommand myCommand = connection.myConnection.CreateCommand();
            myCommand.CommandText = "INSERT INTO customer (custcode, custgroupcode, startdate, taxid, taxrequired, custstatuscode, remark, createdate, createby) VALUES  " + " ('" + this.txtcustcode.Text + "','" + this.txtcustgroupcode.SelectedValue + "','" + this.txtstartdate.Value + "','" + this.txttaxid.Text + "','" + taxrequire + "','" + this.txtcuststatuscode.SelectedValue + "','" + bow + "','" + DateTime.Now + "','" + bow + "') ";
            myCommand.CommandType = CommandType.Text;
            myCommand.ExecuteNonQuery(); 
            connection.myConnection.Close();
            // close connect database

            MessageBox.Show("Save Successfully");

            this.Hide();
            Customer f = new Customer();
            f.Show();               
        }

        private void txtcustgroupcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtcustcode.Text = "";
            int NewId = 1;
            string strnewid = " ";
            object obj = newid(); //DataSet Invoke from DBConnection class
            if (obj.ToString() != "")
            {
                NewId = Convert.ToInt32(obj.ToString()) + 1;
                strnewid = NewId.ToString();
                strnewid = strnewid.PadLeft(8, '0');
                txtcustcode.Text = " '"+ txtcustgroupcode.SelectedValue.ToString() + "' " + strnewid;
            }
            else
            {
                txtcustcode.Text = " '" + txtcustgroupcode.SelectedValue.ToString() + "' " + " 00000001" ;
            }

            //txtcustcode.Text = txtcustgroupcode.SelectedValue.ToString();   
            // get values from combobox[txtcustgroup] to textbox[txtcustcode]
        }


    }

}



frm



Tag : .NET, Ms SQL Server 2008, Win (Windows App), C#, Windows







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-02-17 09:42:34 By : flintstone View : 2087 Reply : 4
 

 

No. 1



โพสกระทู้ ( 119 )
บทความ ( 0 )



สถานะออฟไลน์


ผลว่าปัญหาอาจเกิดจาก obj อาจมีค่าเป็น Null(ค่าว่าง) ลองเอาชุดนี้ไปดูนะครับ
Code (C#)
public string newid()
        {
            string AtCnt;
            SqlConnection myConnection = default(SqlConnection);
            connection NewConnection = new connection();
            NewConnection.Connection_Today();
            connection.myConnection.Open();

            SqlCommand myCommand = connection.myConnection.CreateCommand();
            String SQL_String = "SELECT MAX(custcode) FROM customer WHERE custgroupcode = '" + txtcustgroupcode.SelectedValue.ToString() + "' ";
            SqlCommand  MyCM = new SqlCommand(SQL_String, myConnection);
            object MaxID = MyCM.ExecuteScalar();

        	if (object.ReferenceEquals(MaxID, DBNull.Value)) {
        		AtCnt = txtcustgroupcode.SelectedValue + "000000001";
	        } else {
		        int ID = (MaxID as string).Substring(2);
                ID = ID + 1;
		        AtCnt = txtcustgroupcode.SelectedValue + ID.ToString("000000000");
	        }
            connection.myConnection.Close();
            return AtCnt;
        }


ปล. ปกติใช้ VB เป็นหลัก เขียน C# อาจมีผิดพลาดบ้าง วิเคราะห์เอานะครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 23:51:22 By : base3000
 


 

No. 2



โพสกระทู้ ( 84 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 1 เขียนโดย : base3000 เมื่อวันที่ 2014-02-17 23:51:22
รายละเอียดของการตอบ ::
ขอบคุณนะครับผมก็มือใหม่ C# งงไปหมดแล้วตอนนี้ มันติด error อีกหน่อยครับ รบกวนด้วยนะครับ

ตรงบรรทัด มันแดงครับ มันบอกว่าแปลง string to int ไม่ถูกประมาณนั้น
Code (C#)
int ID = (MaxID as string).Substring(2);


ผมลองปรับเป็นประมาณนี้ ก็มั่วเอาแหละครับ
Code (C#)
string nID = MaxID.ToString().Substring(2);
                int ID = Convert.ToInt32(nID);


แต่ก็ยังติด error :: An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: ExecuteScalar: Connection property has not been initialized.
If there is a handler for this exception, the program may be safely continued.


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-18 08:50:40 By : flintstone
 

 

No. 3



โพสกระทู้ ( 119 )
บทความ ( 0 )



สถานะออฟไลน์


An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: ExecuteScalar: Connection property has not been initialized.
If there is a handler for this exception, the program may be safely continued.

เข้าใจว่ามีปัญหามาตั้งแต่ตัวแปร myConnection แล้ว ผลไม่ค่อยเก่ง C# รอผู้รู้ท่านอื่นมาช่วยชี้แนะละกันนะครับ ขอให้โชคดี
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-18 09:10:18 By : base3000
 


 

No. 4



โพสกระทู้ ( 84 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 3 เขียนโดย : base3000 เมื่อวันที่ 2014-02-18 09:10:18
รายละเอียดของการตอบ ::
ได้แล้วครับ ตามนี้เลย ผมลืมไปว่า ตัว myConnection ของผมอ้างมาจาก Class Connection ขอบคุณมากๆครับ

Code (C#)
string AtCnt;
        public string newid()
        {
            
            SqlConnection myConnection = default(SqlConnection);
            connection NewConnection = new connection();
            NewConnection.Connection_Today();
            connection.myConnection.Open();

            SqlCommand myCommand = connection.myConnection.CreateCommand();
            String SQL_String = "SELECT MAX(custcode) FROM customer WHERE custgroupcode = '" + txtcustgroupcode.SelectedValue.ToString() + "' ";
            SqlCommand MyCM = new SqlCommand(SQL_String, connection.myConnection);
            object MaxID = MyCM.ExecuteScalar();

            if (object.ReferenceEquals(MaxID, DBNull.Value))
            {
                AtCnt = txtcustgroupcode.SelectedValue + "00000001";
            }
            else
            {
                string nID = MaxID.ToString().Substring(2);
                int ID = Convert.ToInt32(nID);
                ID = ID + 1;
                AtCnt = txtcustgroupcode.SelectedValue + ID.ToString("00000000");
            }
            connection.myConnection.Close();
            return AtCnt;
        }


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-18 10:00:37 By : flintstone
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ทำ AUTO ID แล้วติด ERROR System.FormatException อ่ะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่