i have one form ,,, this form contain 2 textboxs (CustNo , Full Name) and 1 Button its function to open connection and insert Data into Database with Table Called MainTable1 this table Include 2 Columns (CustNo,FullName)
i want make an automatic Counter with new number for each FullName i will insert from the textbox and i want the CustNo Textbox is showe up on my form this new number thats also will Insert to The Table
it will be looklike that scenario
am the customer and open the insert win form and its give me CustNo textbox with automatic number and i will write the FullName only and both of this data will inserted when i click the insert button to the database ,,, the automatic number and the Fullname i wrote by my hand and i want then in the same row because this number will be the FullName Refrence
ok hope that i explained well what i need thanks anyway
Kirtan PatelPosted May 9, 2009, 11:38 AM
Happy Coding :)
Hesham HemdanPosted May 9, 2009, 6:34 AM
okay thats good can you showe me the right way to do a query with this Identity_Current
my table named : MainTable1
the column that i want is called : CustNo
is it will be like that
SELECT IDENT_CURRENT ('MainTable1')
Kirtan PatelPosted May 9, 2009, 1:30 AM
but id Auto numbers are Not Numeric Ie GUID you have to use
IDENT_CURRENT
Hesham HemdanPosted May 6, 2009, 4:54 PM
look sorry because i reply late ,,, but look i dont understand you well but i try to use what u said
private
void MainInsertForm_Load(object sender, EventArgs e){
string conn2 = @"Data Source=HEKO-EE77E7BBDC\SQLEXPRESS;Initial Catalog=Zanussi;Integrated Security=True;Pooling=False"; string qry2 = @"SELECT IDENT_CURRENT FROM MainTable1"; SqlDataAdapter da = new SqlDataAdapter(qry2, conn2); DataSet ds = new DataSet();da.Fill(ds,
"MainTable1");CustNoLabel.DataBindings.Add(
"text", ds, "CustNo.MainTable1");}
Hesham HemdanPosted May 7, 2009, 6:44 PM
i want thank you so much for your time
you answer it but this good way but i find this solution its simple more i think
private
void MainInsertForm_Load(object sender, EventArgs e){
string connstring = @"Data Source=HEKO-EE77E7BBDC\SQLEXPRESS;Initial Catalog=Zanussi;Integrated Security=True;Pooling=False"; string queery = @"Select CustNo FROM MainTable1 WHERE CustNo = (Select max(CustNo)FROM MainTable1)"; SqlConnection coni = new SqlConnection(connstring); SqlDataAdapter da = new SqlDataAdapter(queery, coni); DataSet ds = new DataSet();da.Fill(ds,
"MainTable1");CustNo.DataBindings.Add(
"text",ds,"MainTable1.CustNo"); int total; int add = 1; int text = Convert.ToInt32(CustNo.Text);total = add + text;
CustNo.Text = total.ToString();
}
by this way the Textbox bind the latets max value in the automatic number column and i made the addation operation simply like that thank you so much
Kirtan PatelPosted May 7, 2009, 4:45 PM
con.Open();
SqlCommand comm = new SqlCommand("SELECT IDENT_CURRENT ('MainTable1')", con);
comm.CommandType = CommandType.Text;
int lastAutoNumberValue;
//Throws Exception When Database is Blank so enclose Code with Try Catch
try
{
lastAutoNumberValue = Convert.ToInt32(comm.ExecuteScalar());
}
catch(Exception ex)
{
lastAutoNumberValue = 0;
}
int nextAutonumber = lastAutoNumberValue + 1;
// Set Label text to Net AutoNumber Value
label1.Text = "Next Auto Number :" + nextAutonumber;
Hesham HemdanPosted May 6, 2009, 5:01 PM
so when i run it by that way its gives me a exception i thin maybe with stored procedure it will work but i dont know how that stored procudres will handle that
can you help me to tell me how to make the query exactly and here is the exact data am using
the tablename = MainTable1
2 columns = (CustNo) (FullName)
what i want is show when i start to run the insert form the IDENT_CURRENT as you told me i think and i will + 1 to show the number will be the result of this current insert operation i think its become clear but i need help about how to retrieve the CustNo automatic value
Kirtan PatelPosted May 5, 2009, 11:29 PM
use sql Query
select IDENT_CURRENT('
after that Show it on Label With Last Autonumber + 1
Happy Coding :)