这是我的链接数据库的源代码 
<script language="C#" runat="server">
   DataSet ds;
   DataRow dr;
   String newsid;
       void Page_load(Object sender, EventArgs e)
       {
           SqlConnection MyConnection;
           String ConnStr, strCon;
           newsid = Request.Params["id"];
           MyConnection = new SqlConnection("server=localhost;database=news;uid=sa;pwd=''");
           MyConnection.Open();
           String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= " + newsid;
           ds = new DataSet();
           SqlDataAdapter myCommand = new SqlDataAdapter(strCom,MyConnection);
           myCommand.Fill(ds,"news");
           dr = ds.Tables["news"].Rows[0];
           strCon = "SELECT counter FROM news WHERE id = " + newsid;
           SqlCommand myCommand2 = new SqlCommand(strCon,MyConnection);
           SqlDataReader reader = myCommand2.ExecuteReader();
           reader.Read();
           int i = reader.GetInt32(0);
           i ++;
           reader.Close();
           strCon = "UPDATE news SET counter= " + i.ToString() + " WHERE(id= " + newsid+ ")";
           myCommand2.CommandText = strCon;
           myCommand2.ExecuteNonQuery();
           MyConnection.Close(); 
       }
   行 21:            ds = new DataSet();
行 22:            SqlDataAdapter myCommand = new SqlDataAdapter(strCom,MyConnection);
行 23:            myCommand.Fill(ds,"news");
行 24:            dr = ds.Tables["news"].Rows[0];
行 25:            strCon = "SELECT counter FROM news WHERE id = " + newsid;
 

解决方案 »

  1.   

    行  23:                        myCommand.Fill(ds,"news");   红色显示高手们 50分 求救
      

  2.   

    MyConnection = new SqlConnection("server=localhost;database=news;uid=sa;pwd=");
      

  3.   

    newsid 是数字吗? 不是得这样 
    String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= '" + newsid + "'";
    还有你的库名和表名是一样的阿?
    数据库连接字符串里是库名,SQL查询语句里是表名,别搞错了
      

  4.   

    你哪行报错,是
    strCon = "SELECT counter FROM news WHERE id = " + newsid;
    这一行吗你的数据库里id是什么类型,如果是int型,前面newsid = Request.Params["id"];需要转换下类型,如果是varchar类型,语句这样写strCon = "SELECT counter FROM news WHERE id = '" + newsid + "'";
      

  5.   

    reader.Read();
               int i = reader.GetInt32(0);
               i ++;
    ////
    这是在干嘛?
    while(reader.Read())
    {
       int i = reader.GetInt32(0);
       i ++;
       //上面的2行是不是要改下?
    }
      

  6.   

    所有字符串要谨慎处理。例如,假设id是 int 类型的:   ....= "SELECT counter FROM news WHERE id = " + newsid;应该写为:   ....= "SELECT counter FROM news WHERE id = " + int.Parse(newsid).ToString();
      

  7.   

    String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= '" + newsid+"';
      

  8.   

    String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= '" + newsid+"'";
     在每个这样的语句后面都加上 +"'" 这点,newsid 前面的"加个' ,你的sql语句有问题。
    strCon = "SELECT counter FROM news WHERE id = '" + newsid+"'";
     strCon = "UPDATE news SET counter= '" + i.ToString() + "' WHERE(id='"+newsid+"')";
    我自己认为是这样,你自己改改吧,反正sql语句肯定有问题。
      

  9.   

    不好意思,上面错了
    String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= '" + newsid+"'";
      

  10.   

    而如果id是varchar类型的,则应该写为:   ....= "SELECT counter FROM news WHERE id = '" + newsid.Replace("'","''")+"'";
      

  11.   

    同意楼上String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id= " + newsid;strCon = "UPDATE news SET counter= " + i.ToString() + " WHERE(id= " + newsid+ ")";这里也是一样没仔细看,估计还有错误,是你自己写的代码吗,如果不是,还是先找本基础的书来看下吧
      

  12.   

    id 也是 int型
    急于求成了
    5
      

  13.   

    按照zhuanshen712  的意见改了
    又出现:错误
    在位置 0 处没有任何行。
    行 22:            SqlDataAdapter myCommand = new SqlDataAdapter(strCom,MyConnection);
    行 23:            myCommand.Fill(ds,"news");
    行 24:            dr = ds.Tables["news"].Rows[0];
    行 25:            strCon = "SELECT counter FROM news WHERE id = " + int.Parse(newsid).ToString();
    行 26:            SqlCommand myCommand2 = new SqlCommand(strCon,MyConnection);
     行 24:            dr = ds.Tables["news"].Rows[0];  加红显示是不是有进步了
      

  14.   

    if(ds.Tables["news"].Rows.Count == 0)
         return;
    dr = ds.Tables["news"].Rows[0];
      

  15.   

    谢谢楼上
    又有 错误了 
    %%%%%~~~~~未将对象引用设置到对象的实例。 
    行 182:    <td style="width: 728px; text-align: left; height: 20px;" align="center">
    行 183:    <%=dr["biaoti"] %>//加红
    行 184:    </td></tr>
    行 185:    <tr>我在《body》里直接用dr[“”] 显示
    这样不行么?
      

  16.   

    不可以那样;你在后台代码里定义一个protected类型的string 变量
    protected string strBiaoti ;
    strBiaoti  = dr["biaoti"].ToString();
    页面里:<%=strBiaoti %>
      

  17.   

    sbqcel(活死人) :我前后台没有分开 、直接在连接数据库的代码里写就可以吧?不知道怎么写啊 
    ~~~
      

  18.   

    我的代码这样的
    怎么加阿 
    求救
    50分 
    救人于水火 
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>新闻详细信息</title>
       <script language="C#" runat="server">
       DataSet ds;
       DataRow dr;
         
           String newsid;
           void Page_load(Object sender, EventArgs e)
           {
               SqlConnection MyConnection;
               String ConnStr, strCon;
               newsid = Request.Params["id"];
               MyConnection = new SqlConnection("server=localhost;database=news;uid=sa;pwd=''");
               MyConnection.Open();
               String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id=  '" + newsid+"'";
               ds = new DataSet();
               SqlDataAdapter myCommand = new SqlDataAdapter(strCom,MyConnection);
               myCommand.Fill(ds,"news");
               if (ds.Tables["news"].Rows.Count == 0)
                   return;
               dr = ds.Tables["news"].Rows[0];
               strCon = "SELECT counter FROM news WHERE id = '" + newsid + "'";
               SqlCommand myCommand2 = new SqlCommand(strCon,MyConnection);
               SqlDataReader reader = myCommand2.ExecuteReader();
               reader.Read();
               int i = reader.GetInt32(0);
               i ++;
               reader.Close();
               strCon = "UPDATE news SET counter= " + i.ToString() + " WHERE(id=' " + newsid+ "')";
               myCommand2.CommandText = strCon;
               myCommand2.ExecuteNonQuery();
               MyConnection.Close(); 
       
       </script>
         
        
    <style type="text/css">
    <!--
    a {
    font-size: 12px;
    color: #333333;
    }
    a:visited {
    color: #000000;
    }
    a:hover {
    color: #C8180B;
    }
    .STYLE1 {font-size: 10px}
    .STYLE3 {font-size: 12px}
    .STYLE5 {
    color: #EA0100;
    font-weight: bold;
    font-size: 18px;
    }
    .STYLE6 {font-weight: bold}
    #Layer1 {
    position:absolute;
    left:249px;
    top:-21px;
    width:387px;
    height:54px;
    z-index:1;
    }
    .STYLE9 {color: #C53131}
    .STYLE15 {color: #B51021}
    .STYLE16 {font-family: "华文细黑"}
    .STYLE17 {
    color: #333333;
    font-weight: bold;
    }
    .STYLE18 {font-family: "宋体"}
    .STYLE19 {color: #FFFFFF}
    .STYLE20 {font-size: 10px; color: #FFFFFF; }
    body {
    margin-top: 0cm;
    background-image: url(%E5%9B%BE%E7%89%87+Flash/backgroun.gif);
    }
    .STYLE23 {font-family: "华文琥珀"; color: #CF0535;}
    .STYLE24 {color: #F1F1F1}
    .STYLE25 {
    font-size: 18px;
    font-family: "宋体";
    font-weight: bold;
    color: #282425;
    }
    .STYLE26 {color: #333333; }
    .STYLE28 {
    font-weight: bold;
    color: #B51021;
    font-size: 18px;
    font-family: "华文琥珀";
    }
    body {
    margin-left: 120px;
    margin-top: 0px;
    margin-right: 120px;
    background-image: url(images/backgroun.gif);
    }-->
    </style>
    </head>
    <body>
      

  19.   

    还是这样:<strong>未将对象引用设置到对象的实例。</strong> 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 183:    <tr>
    行 184:    <td style="width: 728px; text-align: left; height: 20px;" align="center">
    行 185:    <%=dr["biaoti"] %>
    行 186:    </td></tr>
    行 187:    <tr>
     
      

  20.   

    看了你的代码我真的不知道你要干嘛;
    String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id=  '" + newsid+"'";
               ds = new DataSet();
               SqlDataAdapter myCommand = new SqlDataAdapter(strCom,MyConnection);
               myCommand.Fill(ds,"news");
               if (ds.Tables["news"].Rows.Count == 0)
                   return;
               dr = ds.Tables["news"].Rows[0];
    //数据取出后下面没用取出的数据
    //下面的查询和上面的同一张表,同样的条件,为什么不在上面一起取出来?
               strCon = "SELECT counter FROM news WHERE id = '" + newsid + "'";
               SqlCommand myCommand2 = new SqlCommand(strCon,MyConnection);
               SqlDataReader reader = myCommand2.ExecuteReader();
    //下面的写发没看到过
               reader.Read();
               int i = reader.GetInt32(0);
               i ++;
    //i准备干嘛也不知道
               reader.Close();
      

  21.   

    基础很差俄
    现在是救急阿
    时间很紧用ASp.net1.0
    很基础的代码,
    帮帮忙吧
    数据库:
    news

    newsdatagrid控件  点击链接 绑定 detail.aspx? id={0}
    detail.aspx 代码如下

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="detail.aspx.cs" Inherits="detail" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>新闻详细信息</title>
       <script language="C#" runat="server">
       DataSet ds;
       DataRow dr;
         
           String newsid;
           void Page_load(Object sender, EventArgs e)
           {
               SqlConnection MyConnection;
               String ConnStr, strCon;
               newsid = Request.Params["id"];
               //MyConnection = new SqlConnection("server=localhost;database=news;uid=sa;pwd=''");
               MyConnection = new SqlConnection("server=localhost;database=news;uid=sa;pwd=");           MyConnection.Open();
               String strCom = "SELECT biaoti,zhaizi,neirong,img,counter,shijian FROM news where id=  '" + newsid + "'";
               ds = new DataSet();
               SqlDataAdapter myCommand = new SqlDataAdapter(strCom, MyConnection);
               myCommand.Fill(ds, "news");
               if (ds.Tables["news"].Rows.Count == 0)
                   return;
               dr = ds.Tables["news"].Rows[0];
               strCon = "SELECT counter FROM news WHERE id = '" + newsid + "'";
               SqlCommand myCommand2 = new SqlCommand(strCon, MyConnection);
               SqlDataReader reader = myCommand2.ExecuteReader();
               reader.Read();
               int i = reader.GetInt32(0);
               i++;
               reader.Close();
               strCon = "UPDATE news SET counter= " + i.ToString() + " WHERE(id=' " + newsid + "')";
               myCommand2.CommandText = strCon;
               myCommand2.ExecuteNonQuery();
               MyConnection.Close();
           }
               
       </script>
         
        
    <style type="text/css">
    <!--
    a {
    font-size: 12px;
    color: #333333;
    }
    a:visited {
    color: #000000;
    }
    a:hover {
    color: #C8180B;
    }
    .STYLE1 {font-size: 10px}
    .STYLE3 {font-size: 12px}
    .STYLE5 {
    color: #EA0100;
    font-weight: bold;
    font-size: 18px;
    }
    .STYLE6 {font-weight: bold}
    #Layer1 {
    position:absolute;
    left:249px;
    top:-21px;
    width:387px;
    height:54px;
    z-index:1;
    }
    .STYLE9 {color: #C53131}
    .STYLE15 {color: #B51021}
    .STYLE16 {font-family: "华文细黑"}
    .STYLE17 {
    color: #333333;
    font-weight: bold;
    }
    .STYLE18 {font-family: "宋体"}
    .STYLE19 {color: #FFFFFF}
    .STYLE20 {font-size: 10px; color: #FFFFFF; }
    body {
    margin-top: 0cm;
    background-image: url(%E5%9B%BE%E7%89%87+Flash/backgroun.gif);
    }
    .STYLE23 {font-family: "华文琥珀"; color: #CF0535;}
    .STYLE24 {color: #F1F1F1}
    .STYLE25 {
    font-size: 18px;
    font-family: "宋体";
    font-weight: bold;
    color: #282425;
    }
    .STYLE26 {color: #333333; }
    .STYLE28 {
    font-weight: bold;
    color: #B51021;
    font-size: 18px;
    font-family: "华文琥珀";
    }
    body {
    margin-left: 120px;
    margin-top: 0px;
    margin-right: 120px;
    background-image: url(images/backgroun.gif);
    }-->
    </style>
    </head>
    <body>
        
        <center>  
     
           
    <table width="790" height="471" border="0" bgcolor="white">
      <!--DWLayoutTable-->
      <tr>
        <td style="border-left-color: #ff0033; border-bottom-color: #ff0033; width: 192px; border-right-color: #ff0033; border-top: #ff0033 2px outset;" bgcolor="#f6f6f6" bordercolor="buttonface">
            <span style="font-size: 10pt; border-bottom-color: #ff0033; border-top-color: #ff0033; border-bottom-style: inset; color: #4e4e4d;">
                &nbsp;<img align="middle" src="images/topleft.gif" style="font-size: 10pt; height: 17px" />公告栏</span></td>
        <td valign="top" style="text-align: right; width: 509px; border-top: #ff0033 2px outset; vertical-align: top;" bgcolor="#f6f6f6" bordercolor="buttonface">
            <span style="border-bottom-color: #ff0033; border-bottom-style: inset;">
                <span
                    style="font-size: 10pt; border-bottom-color: #ff0033; border-bottom-style: inset; color: #4e4e4d;">
                    &nbsp; <a href="javascript:window.print()">打印本页</a>
                </span></span></td>
      </tr>
      <tr>
        <td style="border-left-color: #ff0033; border-bottom-color: #ff0033; width: 192px; border-right-color: #ff0033; height: 130px; border-top: #ff0033 2px outset;">&nbsp;<span style="font-size: 10pt"></span></td>
        <td style="width: 509px; height: 130px; text-align: center; border-top: #ff0033 2px outset; vertical-align: top;">
        <table>
        <tr>
        <td style="width: 728px; text-align: left; height: 20px;" align="center">
        <%=dr["strBiaoti"] %>
        </td></tr>
        <tr>
        <td style="height: 55px; width: 728px;">
        <div> <hr size="1"  width="80%" /><br /><%=dr["shijian"] %></div>
        </td></tr>
        <tr><td style="height: 40px; width: 728px;">
        <% 
            if (dr["img"] != "")
            {
                Response.Write("<img src='" + dr["img"] + "' border='0' align='left' width='20%' height='70'>");
            }%>
            <br /><%=dr["neirong"] %>
        </td></tr>
        
        <tr><td style="width: 728px">
        <br />
            <span style="font-size: 10pt">文章出自:<%=dr["zhaizi"] %></span>
        </td></tr>
        </table>
        <table>
        <tr><td style="width: 428px; text-align: left; height: 13px;" align="center">
            <span style="font-size: 10pt">
        本条新闻被浏览了<%=dr["Counter"] %>次 </span>
         c
        </td>
        <td style="height: 13px">
        <p align="right"><a href="javascript:window.close()">关闭窗口</a></p>
        </td></tr>
        </table>
            </td>
      </tr>
      </table>
    </center>
     
    </body>
    </html>