Hi,
I want to generate a 6 digit sequence number with a prefix PB...
I have a found a solution in code project but its showing error...
public string serialId()
{
string Id = GenerateId("customer_details", "id", 6, "PB", true);
return Id;
}
public string GenerateId(string TableName, string ColumnName, int ColumnLength, string Prefix, bool Padding)
{
string query, con, Id;
con = "Data Source=.;Initial Catalog=login_;Integrated Security=True";
SqlConnection cn = new SqlConnection(con);
int preLength, padLength;
preLength = Convert.ToInt32(Prefix.Length);
padLength = ColumnLength - preLength;
if (Padding == true)
{
query = "SELECT'" + Prefix + "'+REPLACE(STR(MAX(CAST(SUBSTRING(" + ColumnName + "," + Convert.ToString(preLength + 1) + "," + padLength + ")AS INTEGER))+1 ," + padLength + "),'',0)FROM " + TableName;
}
else
{
query = "SELECT'" + Prefix + "'+CAST(MAX(CAST(SUBSTRING(" + ColumnName + "," + Convert.ToString(preLength + 1) + "," + padLength + ")AS INTEGER))+1 AS VARCHAR) FROM " + TableName;
}
SqlCommand com = new SqlCommand(query, cn);
cn.Open();
if (com.ExecuteScalar().ToString() == "")
{
Id = Prefix;
if (Padding == true)
{
for (int i = 1; i <= padLength - 1; i++)
{
Id += "0";
}
}
Id += "1";
}
else
{
Id = Convert.ToString(com.ExecuteScalar());
}
cn.Close();
return Id;
}
In the above code while I debug it throws an exception in the above bold line...
The exception is "invalid column name id"...
can anyone help me in clearing this problem?
Regards,
Sajin A
What I have tried:
I have tried debugging and changing the code many times