通过C#递增自动生成的数字并将其显示在Label中,最后将其插入数据库中 [英] Incrementing the auto generated number thru C# and display it in a Label and finally inserting it in the database

查看:191
本文介绍了通过C#递增自动生成的数字并将其显示在Label中,最后将其插入数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/女士,


我想显示数据库表中递增的数字


假设我在数据库中有下表(带有列):
订单(OrderNo,Item,DateofOrder)

它包含3条记录,最后一个OrderNo =3.
现在,我想在Web表单的文本框或标签之一中显示OrderNo + 1,即在这里,它应该在文本框/标签中显示4.

数据库:

Respected Sir/madam,


I want to display the incremented number from the database table
i.e.

Suppose I have the following table (with columns) in the database:
Order (OrderNo , Item, DateofOrder)

and it contains 3 records with the last OrderNo = 3.
Now, I want to display OrderNo + 1 in one of the text boxes or label in the web form i.e. here, it should display 4 in the text box/label.


Database:

create table ord2(sno int primary key,item nvarchar(50),
dateoforder datetime);

insert into ord2 values(1,'keyboard','01-jul-2011');

insert into ord2 values(2,'mouse','01-sep-2011');

insert into ord2 values(3,'pen drive','01-oct-2011');



在网络表单中:拿3个tgext框和一个按钮



我已经尝试了以下代码,但是会产生一些错误:



In the web form : Take 3 tgext boxes and a single button



I have tried the following code but its generating some error:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Net;


public partial class _Default : System.Web.UI.Page
{

    String con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
    SqlConnection constr; //declare SqlConnection object
    SqlCommand cmd,cmd2;//declare SqlCommand object
    SqlDataReader dr;
    decimal on = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        constr = new SqlConnection(con);//constr store the con value
        constr.Open();
        string s = "select sno from ord2 where dateoforder = (select max(dateoforder) from ord2)";
        cmd = new SqlCommand(s, constr);
        SqlDataReader dr = cmd.ExecuteReader();
        constr.Close();

        if (dr.Read())
        {
            on = dr.GetDecimal(0);
        }
        on = on + 1;
        
        cmd2 = new SqlCommand("insert into ord2 values('" + on + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "')", constr);//cmd is used to send sql statement ta database
        constr.Open();
        cmd.ExecuteNonQuery();
        constr.Close();

        Response.Write("<script>alert('Your data has been successfully submitted')</script>");

    }
}  



错误



ERROR

Server Error in '/Online TMS 11 oct 2011' Application.
--------------------------------------------------------------------------------

Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 


Line 38:         if (dr.Read())
Line 39:         {
Line 40:             on = dr.GetDecimal(0);
Line 41:         }
Line 42:         on = on + 1;
 

Source File: c:\Users\sagar\Desktop\Online TMS 11 oct 2011\Default.aspx.cs    Line: 40

推荐答案

使用
on = dr.GetInt32(0);

而不是

on = dr.GetDecimal(0);


使用dr.GetInt32(0),因为您的sno列是int而不是decimal.
Use dr.GetInt32(0) as your sno column is int not decimal.


亲爱的朋友,
TrashBox trashcount =新的TrashBox();
私有void CountId()
{
字符串trashmaxid;
dt = new DataTable();
dt = trashcount.Maxid();
if(dt.Rows [0] [0] .ToString()!=")
{

trashmaxid =(int.Parse(dt.Rows [0] [0] .ToString())+1).ToString();
trashcount.trashid = int.Parse(trashmaxid);


}
其他
trashmaxid ="1";
lbl.text = trashmaxid;
}

在类文件中:
公共DataTable Maxid()
{
DataTable dt = new DataTable();

DataAcessHelper dashp =新的DataAcessHelper();
SqlCommand cmd = dashp.CreateCommand("sp_Trashmaxid");
dt = dashp.GetDataTable(cmd);

return dt;
}


存储过程:

ALTER PROCEDURE sp_Trashmaxid
/*
(
@ parameter1 int = 5,
@ parameter2数据类型OUTPUT
)
*/
AS
从TrashBoxDetails中选择max(TrashId)
返回


分析并尝试此


问候,

Anilkumar.D
Dear Friend,
TrashBox trashcount = new TrashBox();
private void CountId()
{
string trashmaxid;
dt = new DataTable();
dt = trashcount.Maxid();
if (dt.Rows[0][0].ToString() != "")
{

trashmaxid = (int.Parse(dt.Rows[0][0].ToString()) + 1).ToString();
trashcount.trashid = int.Parse(trashmaxid);


}
else
trashmaxid = "1";
lbl.text= trashmaxid ;
}

in class file:
public DataTable Maxid()
{
DataTable dt = new DataTable();

DataAcessHelper dashp = new DataAcessHelper();
SqlCommand cmd = dashp.CreateCommand("sp_Trashmaxid");
dt = dashp.GetDataTable(cmd);

return dt;
}


stored procedure:

ALTER PROCEDURE sp_Trashmaxid
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
select max(TrashId) from TrashBoxDetails
RETURN


Analize and Try this


Regards,

Anilkumar.D


这篇关于通过C#递增自动生成的数字并将其显示在Label中,最后将其插入数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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