从表中检索数据并提供给网格

时间:2015-06-26 13:49:14

标签: c# asp.net gridview

我正在尝试创建一个包含特定票证的所有信息的网格,其中有很多状态(表中的行),我需要放入网格,这是我正在使用的方法:

调用方法填充网格:

 private void AssemblyGrid()
        {
            //Set Grid
            gridTStatus.DataSource = null;
            gridTStatus.DataBind();

            User loggedUser = (User)HttpContext.Current.Session[SESSIONNAME.LOGGEDUSER];
            // Checa se o usuario tem permissão para utilizar essa funcionalidade
            if ((loggedUser.Login.ToUpper() != ConfigurationManager.AppSettings[APPSETTINGS.SUPPORTUSERS].ToUpper()))
            {
                Response.Redirect("AccessDenied.aspx");

            }
            Clic objClic = GetClic();
            Status returnStatus = new Status();
            List<StatusClicComplete> lstClic = new List<StatusClicComplete>();
            lstClic = ClicManager.SelectStatusClic(objClic,out returnStatus);




            //Set Grid
            gridTStatus.DataSource = lstClic;
            gridTStatus.DataBind();
        }

从datebase检索信息的方法:

public List<StatusClicComplete> SelectStatusClicDB(Clic objClic , out Status returnStatus)
        {


            const string strStoredProcedure = "spSearchClicStatus";

            List<StatusClicComplete> complete = new List<StatusClicComplete>();

            try
            {


                Database database = DatabaseFactory.CreateDatabase(DATABASESETTINGS.CLICDB);
                using (DbCommand dbCommand = database.GetStoredProcCommand(strStoredProcedure))
                {
                    database.AddInParameter(dbCommand, "@iClic", DbType.Int32, objClic.ID);

                    using (IDataReader dataReader = database.ExecuteReader(dbCommand))
                    {
                        while (dataReader.Read())
                        {
                            complete.Add(new StatusClicComplete()
                                             {
                                                 iClic = dataReader.GetInt32(Convert.ToInt32("iClic")),
                                                 iStatus = dataReader.GetInt32(Convert.ToInt32("iStatus")),
                                                 dtDateCreated = dataReader.GetDateTime(Convert.ToInt32("dtDateCreated")),
                                                 iEDV = dataReader.GetInt32(Convert.ToInt32("iEDV")),
                                                 sComments = dataReader.GetString(Convert.ToInt32("sComments"))

                                             }
                                );

                        }
                        dataReader.Close();
                        returnStatus = StatusBuilder.BuildStatus("Success", string.Format("{0} - {1}", MethodBase.GetCurrentMethod().Name), true);

                    }
                }
            }
            catch(Exception exception)
            {

                returnStatus = StatusBuilder.BuildStatus("Error", string.Format("{0} - {1}", MethodBase.GetCurrentMethod().Name, exception.Message), false);

            }
            //TODO NAV8CA   - Escrever tratativa de objeto nulo
            return complete;




        }

StatusClicClass,即列表类型

namespace RB.LA.TER.CLIC.TRANSPORT
{
    [Serializable]
    public class StatusClicComplete
    {
        public int iClic { get; set; }
        public int iStatus { get; set; }
        public DateTime dtDateCreated { get; set; }
        public int iEDV { get; set; }
        public string sComments { get; set; }
    }
}

程序:

PROCEDURE [dbo].[spSearchClicStatus]


@iClic int = NULL

AS

SELECT
        dtDateCreated, iEDV, sComments, sDescription, SC.iStatus
    FROM
        T_STATUS_CLIC SC INNER JOIN T_STATUS S
            ON SC.iSTATUS = S.iSTATUS
    WHERE
        SC.iClic = @iClic
    ORDER BY 
        dtDateCreated



GO

我正在尝试运行此代码,但是我收到错误(输入字符串的格式不正确。)当它输入完整的时候,有人知道我做错了什么?

enter image description here

1 个答案:

答案 0 :(得分:1)

dataReader.GetInt32(Convert.ToInt32("iStatus")) 行是错误的。它应该是

dataReader.GetInt32(4)

dataReader.GetInt32(dataReader.GetOrdinal("iStatus"));

在第一种情况下,直接指定序数。在第二种情况下,我们使用列名计算序数。另外,在使用此方法之前,您最好必须检查IsDBNull,否则您将获得InvalidCastException。因此,更优化的方法将是,

iClic = objClic.ID, // we already have that
iStatus = dataReader.IsDBNull(dataReader.GetOrdinal("iStatus")) ? dataReader.GetInt32(dataReader.GetOrdinal("iStatus")) : 0

P.S:序号位置是SELECT子句中列的位置。它是一个零基指数。在你的选择查询中,iClic有5个 th 位置,因此序数位置为4。