如何在asp.net中获取自动增量id# [英] how to get autoincrement id in asp.net c#

查看:80
本文介绍了如何在asp.net中获取自动增量id#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请一些人告诉我如何在asp.net c#中获取autoincreamented id,我写代码却抛出错误



不能隐式转换类型'对象' 'int'。存在显式转换(您是否错过演员表?)





和代码是

  public   void  gen1()
{
con.Open ();
int a = 0 ;
SqlCommand com = new SqlCommand( select来自类别,con)的max(id);
a = com.ExecuteScalar();
TextBox4.Text = Convert.ToString(a);
con.Close();
}









请帮我纠正。 ..

解决方案

嗨Rani,

    尝试更换

 a = com.ExecuteScalar(); 



with

 a = Conver。 ToInt32(com.ExecuteScalar()); 



检查 SqlCommand.ExecuteScalar方法 [ ^ ]。你需要将该对象强制转换为指定的类型。



它将会运作。

     - -Amit


试试这个,

  public   void  Autogenrate()
{
int r;
尝试
{
con = new SqlConnection( data source = XYZ; user id = ABC; password = ***; initial catalog = Detail);
con.Open();
SqlCommand cmd = new SqlCommand( 选择来自empp1的最大(id),con);
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{

字符串 d = dr [ 0 ]。ToString();
if (d ==
{

id.Text = 1001; // 在文本框中设置名称为id的值

}
else
{

r = Convert.ToInt32(dr [ 0 ]的ToString());
r = r + 1 ;
id.Text = r.ToString();
}
}
con.Close();
con.Open();

cmd.CommandText = insert empp1 values(' + id。文字+ ',' + txtName.Text + ',' + txtCity.Text + ');

cmd.ExecuteNonQuery();
}

catch (例外情况)
{
Response.Write( < script> alert( + ex.Message + )< / script>);
}
最后
{
con.Close();
}
}


你可以在表格中增加id,





点击此处

pls some one tell that how to get autoincreamented id in asp.net c#, i write the code but it throws error

Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)


and code is

public void gen1()
{
    con.Open();
    int a = 0;
    SqlCommand com = new SqlCommand("select max(id) from Category", con);
    a = com.ExecuteScalar();
    TextBox4.Text = Convert.ToString(a);
    con.Close();
}





pls help me to correct...

解决方案

Hi Rani,
    Try replacing

a = com.ExecuteScalar();


with

a = Conver.ToInt32(com.ExecuteScalar());


Check SqlCommand.ExecuteScalar Method[^] which returns object. You need to cast that object to a specified type.

It'll work out.
    --Amit


Try This,

public void Autogenrate()
{
    int r;
    try
    {
      con = new SqlConnection("data source=XYZ; user id=ABC; password=***; initial catalog=Detail");
   con.Open();
        SqlCommand cmd = new SqlCommand("Select max(id) from empp1", con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            string d = dr[0].ToString();
            if (d == "")
            {

                id.Text = "1001";//set the value in textbox which name is id

            }
            else
            {

                r = Convert.ToInt32(dr[0].ToString());
                r = r + 1;
                id.Text = r.ToString();
            }
        }
        con.Close();
        con.Open();

      cmd.CommandText = "insert empp1 values('" + id.Text + "','" +   txtName.Text + "','" + txtCity.Text + "')";

        cmd.ExecuteNonQuery();
    }

    catch (Exception ex)
    {
        Response.Write("<script>alert("+ex.Message+")</script>");
    }
    finally
    {
        con.Close();
    }
}


you can increment the id in table ,


click here


这篇关于如何在asp.net中获取自动增量id#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆