คือว่า ผมต้องการทำ 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
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.
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.