increment with last id , how to get the counted value of particular field in a variable to perform some function using the counted value. {
SqlConnection con = newSqlConnection(@"server=CSE-PC; integrated security=true; Database=Bas");
con.Open();
SqlCommand com = newSqlCommand("select top(1) SUBSTRING(CustomerId,1,0) as Last_Id from devi order by Last_Id desc", con);
strc00 = com.ExecuteScalar().ToString();
com.ExecuteNonQuery();
here i have gave code like this but it does not work any one please give syntax for this question.
if (i == 0) here I need to increment with last id how to get the counted value in a variable.
{
strc00 = "001";
}
else
{
int j = i + 1;
strc00 = "001" + j.ToString();
}
comboBox1.Items.Add(strc00);
i++;
MessageBox.Show("Id Created");
con.Close();
}
}


Syed ShanuRank #87 · 23.7K reputationPosted Apr 9, 2015, 9:34 PM EDT
Hope this will help you.
--Create Sample Table
create table devi (CustomerID varchar(30))
--Insert one sample Record
Insert into devi(CustomerID) Values ('C001')
--Declare Variable
Declare @maxCustID nvarchar(30);
--Store the Max value result to a Variable
Set @maxCustID='C00' + Convert(Varchar(10),(Select MAX(RIGHT(CustomerId, 1))+1 from devi))
-- Display the Max Customer ID
select @maxCustID CustIds
You can also check my article which has related example to save the Item master with incremetn value.
http://www.c-sharpcorner.com/UploadFile/asmabegam/insert-select-update-delete-in-Asp-Net-with-simple-login/
Durga VelusamyRank #4,868 · 321 reputationPosted Apr 9, 2015, 10:30 PM EDT
Syed ShanuRank #87 · 23.7K reputationPosted Apr 9, 2015, 10:33 PM EDT
saravanagopi s23 reputationPosted Apr 9, 2015, 11:09 PM EDT
desc", conn);
conn.Open();
string strc00 = com.ExecuteScalar().ToString();
string substr = strc00.Substring(1, 1);
com.ExecuteNonQuery();
conn.Close();
//In substr you will get the substring value(instead of 1,1 change what you need to split)
//as you entered 1,0 it wont return any value
saravanagopi s23 reputationPosted Apr 9, 2015, 11:14 PM EDT
Syed ShanuRank #87 · 23.7K reputationPosted Apr 9, 2015, 11:21 PM EDT