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 ShanuPosted Apr 10, 2015, 3:21 AM
saravanagopi sPosted Apr 10, 2015, 3:14 AM
saravanagopi sPosted Apr 10, 2015, 3:09 AM
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
Syed ShanuPosted Apr 10, 2015, 2:33 AM
Durga VelusamyPosted Apr 10, 2015, 2:30 AM
Syed ShanuPosted Apr 10, 2015, 1:34 AM
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/