 |
|
ทำ AUTO ID แล้วติด ERROR System.FormatException อ่ะครับ |
|
 |
|
|
 |
 |
|
คือว่า ผมต้องการทำ 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]
}
}
}

Tag : .NET, Ms SQL Server 2008, Win (Windows App), C#, Windows
|
|
 |
 |
 |
 |
Date :
2014-02-17 09:42:34 |
By :
flintstone |
View :
2166 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผลว่าปัญหาอาจเกิดจาก 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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|