向消息显示表中的数据是否在C#中可用 [英] show massage that data in table is availlable or not in C#

查看:54
本文介绍了向消息显示表中的数据是否在C#中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个字段的表(人)..... id,名称和城市.

所以我想从人表中读取ID ...如果表中没有任何数据比按摩显示ID为"1",并且当表中存在数据时……比读取MAX(ID)时,按摩将显示最大(id)+ 1.

OP的答案提供的其他信息:

我正在使用此代码.....

i have table(person) with 3 fields.....id, name and city.

so i want to read id from person table...if table have no any data than massage show id is "1" and when data is present in table......than read max(id) and massage will show max(id) + 1.

Additional Information from OP''s answer:

I m using this code.....

SqlConnection con = new SqlConnection(@"Data Source=ammu\SQLEXPRESS;Initial Catalog=report;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("select max(id) from spot ", con);
SqlDataReader dr = comm.ExecuteReader();
  
if(dr!= null && dr.Read())
{
   string a = dr.GetInt32(0).ToString();
   ins_seri.Text = Convert.ToInt32(a.ToString()) + "1"; 
   dr.Close();
   dr.Dispose();
}
else
{
   ins_seri.Text = "1";
   dr.Close();
   dr.Dispose();
}



但是显示错误........
数据为空.不能在空值上调用此方法或属性."



but it show error........
"Data is Null. This method or property cannot be called on Null values."

推荐答案

try
{
dataset ds = new dataset
string qry = "Select max(id) id from tblperson"
sqlcommand cmd = new sqlcommand(qry,mycon)
dataadapter da = new sqldataadapter(cmd)
da.fill(ds,"tblperson")
if (ds.tables(0).rows.count - 1 > 0)
{
if (ds.tables(0).rows(0).item("id").tostring = "" or ds.tables(0).rows(0).item("id").tostring is nothing )
{
messegebox.show("1")
}
else
{
messegebox.show("ur id is "+ds.tables(0).rows(0).item("id").tostring+ )
}
}
else 
{
messegebox.show("1")
}

}
catch (exception ex)
{
}


这篇关于向消息显示表中的数据是否在C#中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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