如何从数据集中获取值到一个变量?在Web应用程序中 [英] How to get value from dataset to one variable? in web application

查看:77
本文介绍了如何从数据集中获取值到一个变量?在Web应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while循环中有什么错误?

What is an error within while loop?

protected void Page_Load(object sender, EventArgs e)
    {
        string path = "Data Source=192.168.1.14;Initial Catalog=nazer;User ID=sa;Password=admin*123";
        SqlConnection con = new SqlConnection(path);
        con.Open();
        string query = "select MAX(id) from tbl_samp1";
        SqlCommand cmd = new SqlCommand(query, con);        
        SqlDataReader dre=cmd.ExecuteReader();
        while (dre.Read())
        {        
           // int values = int.Parse(dre["id"].ToString());  //pls give any alternate code within while loop.Error occured in this line.
        int values = (int)dre["id"];
        TextBox1.Text = values.ToString();            
        }      
        con.Close();        
    }

推荐答案

看看本文

http://msdn.microsoft.com/en-us/library/haa3afyz (v = vs.90).aspx [
Have A Look At This Article

http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.90).aspx[^]

Accept This Answer If It Has Helped You


在执行while循环之前,您应验证您是否具有以下有效记录:
Before executing while loop, you should validate whether you have valid records as:
while (dre.Read())
            {





if (dre.HasRows)
       {
           while (dre.Read())
           {



优良作法是使用try..catch块来跟踪异常



It is good practice to trace the exception using try..catch block


先生,

是的,正如Sen所说,您必须始终验证您的dr以检查是否有任何行.

您可以尝试将您的dr转换为toString().

谢谢.
Hi Sir,

Yes, as Sen said you must always validate your dr to check if there is a rows or nothing.

And you can try to convert your dr into toString().

Thank You.


这篇关于如何从数据集中获取值到一个变量?在Web应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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