dear Programmer,
string was generate code and give error for second time as input string was not in correct format-
code behind-
private string GenerateNewCID(string connection, string query)
{
string newCID = string.Empty;
using (SqlConnection cnn = new SqlConnection(connection))
{
cnn.Open();
SqlCommand cmd = new SqlCommand(query, cnn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string i = dr[0].ToString();
if (string.IsNullOrEmpty(i))
{
newCID = DateTime.Now.ToString("yyMM") + ddlstu.SelectedValue + "0001";
}
else
{
i = i.Substring(4);
int j = Convert.ToInt32(i);
j = j + 1;
newCID = DateTime.Now.ToString("yyMM") + ddlstu.SelectedValue + j.ToString().PadLeft(4, '0');
}
}
cnn.Close();
}
return string.Concat(newCID);
}
input code behind-
protected void btnsave_Click(object sender, EventArgs e)
{
string connstr = ConfigurationManager.ConnectionStrings["xxxx"].ConnectionString;
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
cnn.Open();
string previousCIDquery = "SELECT MAX(RegdNo) RegdNo FROM StudentRegd";
string newCID = GenerateNewCID(connstr, previousCIDquery);
SqlCommand cmd = new SqlCommand("insert into stutablevalues('" + newCID + "','" + null + "','" + ddlstunamecat.SelectedItem + "','" + stuFN.Text + "','" + stuMN.Text + "','" + stuLN.Text + ",'" + stulandmark.Text + "','" + stucity.Text + "','"+DateTime.Now+"')", cnn);
cmd.ExecuteNonQuery();
cnn.Close();