ankit gaur

ankit gaur

  • 1.4k
  • 298
  • 32.8k

ParameterId is not inserting in a incremental order in sql

Aug 1 2018 4:52 AM
My C# code
 
private string AutoIncrementID()
{
SqlConnection con = new SqlConnection();
myconn.Open();
SqlCommand cmd = new SqlCommand("Select *,ParameterId = 'P' + convert(varchar(10),row_number() over (partition by ItemId order by ParaId)) from tblMatParameter", myconn);
SqlDataReader dr = cmd.ExecuteReader();
string ParameterId = null;
if (dr.Read())
{
ParameterId = dr[0].ToString();
myconn.Close();
}
return ParameterId;
}
protected void Save(object sender, EventArgs e)
{
foreach (GridViewRow gvr in Gridview1.Rows)
{
//Connection
TextBox Parameter = gvr.FindControl("txtPara") as TextBox;
string txtPara = Parameter.Text;
string ParameterId = AutoIncrementID();
//int idLimit = 2;
string ParaNo = "" + ("P" + ParameterId);
SqlCommand cmd = new SqlCommand("INSERT INTO [ElixirERP].[ElixirAdmin].[tblMatParameter](ParameterId,Category,SubCategory,Brand,Item,Parameter,ItemId)" +
"VALUES('" + ParaNo + "',@ddlCategory,@ddlSubCategory,@ddlBrand,@ddlItem,'" + txtPara + "',(Select ItemId From [ElixirERP].[ElixirAdmin].[tblMatItem] where ItemId = '" + ddlItem.SelectedValue + "'))");
 
 Sql Table 
 

Answers (6)