当前位置:编程学习 > C#/ASP.NET >>

从索引 0 处开始,初始化字符串的格式不符合规范。

public class Common
{
public SqlConnection GetConnection() 
    {
        string myStr = ConfigurationManager.AppSettings["ConStr"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

   public DataSet GetDataSet(string str,string tableName) 
    {
        SqlConnection myConn = GetConnection();
        SqlDataAdapter adapt = new SqlDataAdapter(str,tableName);
        DataSet ds = new DataSet();
        adapt.Fill(ds,tableName);
        return ds;
    }
}

deflaut.aspx.cs
Common c = new Common();
    protected void Page_Load(object sender, EventArgs e)
    {
dlLeaveWord.DataSource = c.GetDataSet(str, "tb_leaveword");
        dlLeaveWord.DataBind();
    }



从索引 0 处开始,初始化字符串的格式不符合规范。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentException: 从索引 0 处开始,初始化字符串的格式不符合规范。

源错误: 


行 65:     {
行 66:         SqlConnection myConn = GetConnection();
行 67:         SqlDataAdapter adapt = new SqlDataAdapter(str,tableName);
行 68:         DataSet ds = new DataSet();
行 69:         adapt.Fill(ds,tableName);
 

源文件: d:\HR\2119\App_Code\Common.cs    行: 67 

堆栈跟踪: 


[ArgumentException: 从索引 0 处开始,初始化字符串的格式不符合规范。]
   System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +4867618
   System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +129
   System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +98
   System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +55
   System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +24
   System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +200
   System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) +62
   System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +4
   System.Data.SqlClient.SqlConnection..ctor(String connectionString) +24
   System.Data.SqlClient.SqlDataAdapter..ctor(String selectCommandText, String selectConnectionString) +40
   Common.GetDataSet(String str, String tableName) in d:\HR\2119\App_Code\Common.cs:67
   MasterPage.DlLeaveWordBind() in d:\HR\2119\MasterPage.master.cs:53
   MasterPage.Page_Load(Object sender, EventArgs e) in d:\HR\2119\MasterPage.master.cs:22
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

 


--------------------编程问答-------------------- 调试。。。 --------------------编程问答-------------------- ConfigurationManager.AppSettings["ConStr"]有内容吗,调试看看吧 --------------------编程问答--------------------
public DataSet GetDataSet(string str,string tableName)  
  {
  SqlConnection myConn = GetConnection();
  SqlDataAdapter adapt = new SqlDataAdapter(str,tableName);
  DataSet ds = new DataSet();
  adapt.Fill(ds,tableName);
  return ds;
  }

其它不说,你这段就有问题,myConn要先Open,然后new SqlDataAdapter(str,tableName);tableName的位置应该是myConn,str的位置是SqlCommandText --------------------编程问答-------------------- SqlDataAdapter 不需要open connection

这个错误估计是SQL的问题,你调试看看sql。 --------------------编程问答-------------------- 这句SqlDataAdapter adapt = new SqlDataAdapter(str,tableName);报错,其实就是调用dlLeaveWord.DataSource = c.GetDataSet(str, "tb_leaveword");时传得第二个参数不对,应该是一个数据连接,改成:
dlLeaveWord.DataSource = c.GetDataSet(str, myConn);
前提是str是个合法的sql语句。 --------------------编程问答--------------------
引用 4 楼  的回复:
SqlDataAdapter 不需要open connection

这个错误估计是SQL的问题,你调试看看sql。


我到现在居然不知道SqlDataAdapter不需要open connection,惭愧,
如果我SqlDataAdapter sda=new SqlDataAdapter(cmd);cmd里面的Conn要先Open吗?
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,