if the identity is set to true then it autogenarate the field value.
how to get that value in a dropdownlist in c#.net.
plz help me.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Apr 3, 2013, 11:47 PM
hi,
try following code.
SqlConnection con = new SqlConnection("your connectionstring");
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
// write your query instead of my department master query.
command.CommandText = "Insert into DepartmentMaster values('newName','new decription');select @id = SCOPE_IDENTITY()";
command.Connection = con;
con.Open();
command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;
int res = command.ExecuteNonQuery();
var newValue = command.Parameters["@id"].Value;
con.Close();
hope this will help you.
Jignesh TrivediPosted Apr 5, 2013, 11:35 PM
yes,
to avoid SQL injection, use stored procedure or parameterized query.
ipsita paniPosted Apr 5, 2013, 8:57 PM
Sreejesh SPPosted Apr 4, 2013, 1:36 AM
try following code.
SqlConnection con = new SqlConnection("your connectionstring");
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = "Insert into DepartmentMaster values('newName','new decription');set @id = @@IDENTITY";
command.Connection = con;
con.Open();
command.Parameters.Add("@id", DbType.Int32).Direction = ParameterDirection.Output;
int res = command.ExecuteNonQuery();
var newValue = command.Parameters["@id"].Value;
con.Close();
thanks
Sreejesh
Satyapriya NayakPosted Apr 4, 2013, 12:35 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dynamic_increment_using_Dropdown
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("Select");
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
DropDownList1.Items.Add(count);
con.Close();
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee values('" + DropDownList1.SelectedItem.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
}
}
}