+ -
当前位置:首页 → 问答吧 → ASP.NET MVC3查询功能实现的问题

ASP.NET MVC3查询功能实现的问题

时间:2011-06-16

来源:互联网

我的查询功能是通过调用SQL server management studio2008的对应表的存储过程来实现指定查询的。具体过程如下:查询页面Query.cshtml,与查询有关的RAZOR: <li>@Html.ActionLink("查询", "Show", new { idd = "T003" })</li>.然胡跳转到HomeController类下的Show方法,其查询相关代码是:
  DAL.Ticket.ticketClass tc = new DAL.Ticket.ticketClass();
  public ActionResult Show(string idd)
  {
  var ticket = tc.GetSngtickets(idd).ToList();//就是此处报异常
  return View(ticket);
  }
DAL里面的Ticket文件夹下的ticketClass的GetSngtickets方法为:
 MsSql msh = new MsSql();
 public List<Models.train.Tickets> GetSngtickets(string idd)
  {
  List<Models.train.Tickets> ticketslist = new List<Models.train.Tickets>();

  string[] strParams = new string[] { "Tno"};
  object[] strValus = new object[] { idd};
  DataSet ds = msh.usuSelProc("sp_selSngStu", "Conn", strParams, strValus);

  if (ds != null && ds.Tables[0].Rows.Count > 0)
  {
  for (int ii = 0; ii < ds.Tables[0].Rows.Count; ii++)
  {
  Models.train.Tickets tickets = new Models.train.Tickets();
  tickets.Tno = ds.Tables[0].Rows[ii][0].ToString();
  tickets.Type = ds.Tables[0].Rows[ii][1].ToString();
  tickets.TStart = ds.Tables[0].Rows[ii][2].ToString();
  tickets.TEnd = ds.Tables[0].Rows[ii][3].ToString();
  tickets.Stime = ds.Tables[0].Rows[ii][4].ToString();
  tickets.Atime = ds.Tables[0].Rows[ii][5].ToString();
  tickets.Utime = ds.Tables[0].Rows[ii][6].ToString();
  //tickets.Price = (float)ds.Tables[0].Rows[ii][7];
  tickets.StandLeft = (int)ds.Tables[0].Rows[ii][8];
  // tickets.Seatleft = (int)ds.Tables[0].Rows[ii][9];
  ticketslist.Add(tickets);
  }
  return ticketslist;
  }
  else
  {
  return null;
  }
而Models.train.下的Tickets类的方法与数据表对应,包含Tno、Ttype、TStart、TEnd、Stime、Atime、Utime、Price、StandLeft、SeatLeft,里面提供get,set。
MsSql类中的 usuSelProc方法如下:
 public DataSet usuSelProc(string storename, string Conn, string[] strParams, object[] strValues)
  {
  DataSet ds = new DataSet();
  SqlConnection MyConn = new SqlConnection(StaticDataConnClass.getSQLDataConn(Conn));
  try
  {
  if ((strParams != null) && (strParams.Length != strValues.Length))
  {
  ds = null;
  }
  else
  {
  MyConn.Open();
  SqlDataAdapter MyAd = new SqlDataAdapter(storename, MyConn);//设置SQL 命令

  MyAd.SelectCommand.CommandType = CommandType.StoredProcedure;//调用存储过程

  if (strParams != null)
  {
  for (int i = 0; i < strParams.Length; i++)
  MyAd.SelectCommand.Parameters.AddWithValue(strParams[i], strValues[i]);

  }
  SqlParameter rst = MyAd.SelectCommand.Parameters.Add("@result", SqlDbType.Int);
  rst.Direction = ParameterDirection.Output;

  MyAd.Fill(ds, "newtable");
  }

  }
  catch
  {
  ds = null;
  }
  finally
  {
  MyConn.Close();
  MyConn.Dispose();
  }
  return ds;
  }
StaticDataConnClass类的方法getSQLDataConn如下:
  internal static string getSQLDataConn(string connValue)
  {
  string conn = ConfigurationManager.ConnectionStrings[connValue].ToString();
  return conn;
  }
而根目录下的web.config包含
<connectionStrings >
  <add name ="Conn" connectionString ="server=(local);uid=sa;pwd=123456;database=Traintickets;" providerName ="System.Data.SqlClient"/>
  </connectionStrings>。另外我在Traintickets数据库中建立的sp_selSngStu存储过程在SQL SEVER里面执行没问题,可以实现按Tno来进行查询。
启动运行项目之后,总是在Show方法的 var ticket = tc.GetSngtickets(idd).ToList();处报如下异常:用户代码未处理ArgumentNullException 值不能为null 参数名source。想请各路高手指教呀!感激不尽呀!

作者: openyoumind   发布时间: 2011-06-16

Show方法对应的视图Razor代码未:
@model IEnumerable<TicketWeb.Models.train.Tickets>

@{
  ViewBag.Title = "Show";
}

<h2>Show</h2>

<p>
  @Html.ActionLink("Create New", "Create")
</p>
<table>
  <tr>
  <th>火车信息表</th>
  <th>
  车次号
  </th>
  <th>
  列车类型
  </th>
  <th>
  始发站
  </th>
  <th>
  终点站
  </th>
  <th>
  出发时间
  </th>
  <th>
  到达时间
  </th>
  <th>
  用时
  </th>
  <th>
  价格
  </th>
  <th>
  站票数量
  </th>
  <th>
  坐票数量
  </th>
  </tr>

@foreach (var item in Model) {
  <tr>
  <td>
  @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
  @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
  @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
  </td>
  <td>
  @item.Tno
  </td>
  <td>
  @item.Ttype
  </td>
  <td>
  @item.TStart
  </td>
  <td>
  @item.TEnd
  </td>
  <td>
  @item.Stime
  </td>
  <td>
  @item.Atime
  </td>
  <td>
  @item.Utime
  </td>
  <td>
  @item.Price
  </td>
  <td>
  @item.StandLeft
  </td>
  <td>
  @item.SeatLeft
  </td>
  </tr>
}

</table>

作者: openyoumind   发布时间: 2011-06-16