为什么不可用总是返回?

时间:2015-10-23 14:29:27

标签: c# if-statement datatable

我必须在这里遗漏一些非常简单的东西,但为什么总是会返回“不可用”?我也试过西雅图伦纳德的How to Get a Specific Column Value from a DataTable?,但仍然没有运气。

                    int ID;
        string sQuery = "INSERT INTO dbo.tbl_personDetails " +
             "(" +
                 "EmployeeID " +
                 ",Title " +
                 ",Name " +
             ")" +
         "VALUES" +
             "(" +
                 "@EmployeeID " +
                 ",@Title " +
                 ",@Name " +
             ") " ;

    SqlCommand comm = new SqlCommand(sQuery);

        comm.Parameters.Add(new SqlParameter("@EmployeeID", ToolTip.getUserName(GetUser.GetNtLoginID())));
        comm.Parameters.Add(new SqlParameter("@Title", ddlTitle2.Text.ToString()));
        comm.Parameters.Add(new SqlParameter("@Name", Name.Text.ToString()));


        ID = DBConnect.InsertRecordGetID(comm, sQuery);
        if (ID > 0)
        {

         //complete the Reference Number
            string divCode;
            string sSql = 
            "SELECT TheDivision FROM       dbo.tbl_Incident_Details   WHERE ID = " + ID;
            DataTable dt = new DataTable();
            dt = DBConnect.DataReaderDataTable(sSql);
            if(dt.Rows.Count > 0)
            {
                  divCode = dt.Rows[0][0].ToString();
            }
            else
            {
                divCode = "Unavailable";
            }

            string TheDate = DateTime.Now.Year.ToString();
            string TheYear = TheDate.Substring(2, 2);
            string RefNo = "FAI/" + ID + "/" + divCode + "/" + TheYear;


            string sSqlRefNo = "UPDATE dbo.tbl_personDetails " +
                                "SET " +
                                    "RefNo = '" + RefNo + "' " +
                                "WHERE " +
                                    "ID = @ID";
                SqlCommand commRefNo = new SqlCommand(sSqlRefNo);

            commRefNo.Parameters.Add(new SqlParameter("@RefNo", RefNo));
            commRefNo.Parameters.Add(new SqlParameter("@ID", ID));

            if (DBConnect.CmdExecute(commRefNo, sSqlRefNo))
            {
                Response.Redirect("~/FileUpload.aspx?id=" + ID);
            }
            else
            {
            }
        }
        else { }

    }

}

有什么建议吗?

编辑:我现在已经发布了完整的功能。 divCode也不是DB中的varchar而不是int。

DT {}     base {System.ComponentModel.MarshalByValueComponent}:{}     CaseSensitive:false     ChildRelations:{System.Data.DataRelationCollection.DataTableRelationCollection}     列:{System.Data.DataColumnCollection}     约束:{System.Data.ConstraintCollection}     DataSet:null     DefaultView:{System.Data.DataView}     DisplayExpression:“”     ExtendedProperties:Count = 0     HasErrors:false     IsInitialized:是的     区域设置:{zh-CN}     MinimumCapacity:50     命名空间:“”     ParentRelations:{System.Data.DataRelationCollection.DataTableRelationCollection}     字首: ””     PrimaryKey:{System.Data.DataColumn [0]}     RemotingFormat:Xml     行:{System.Data.DataRowCollection}     网站:null     TableName:“”

1 个答案:

答案 0 :(得分:0)

我不知道这是多少答案,但它解决了!我不知道我在星期五下午的想法,但血腥的逻辑并不好,因为我试图在输入之前提取数据,这可能是因为Divisions确实存在但是当我已经存在时这是什么意思用户选择分区的前端的另一部分,所以没有现实世界的实用性!足够的聊天,但这对我来说是这样的:

 //Store what is selected from the Division's ddl
 string areaSelected = ddlArea.Text.ToString();

 //Then
  ID = DBConnect.InsertRecordGetID(comm, sQuery);
    if (ID > 0)
    {

        //complete the Reference Number
        //string divCode;
        //string sSql = 
        //"SELECT TheDivision FROM       dbo.tbl_Incident_Details   //WHERE ID = " + ID;
        //DataTable dt = new DataTable();
        //dt = DBConnect.DataReaderDataTable(sSql);
        //if(dt.Rows.Count > 0)
        //{
        //      divCode = dt.Rows[0][0].ToString();
        //}
        //else
        //{
        //    divCode = "Unavailable";
        //}

        string TheDate = DateTime.Now.Year.ToString();
        string TheYear = TheDate.Substring(2, 2);
        string RefNo = "FAI/" + ID + "/" + areaSelected + "/" + TheYear;

        //The Update and the rest then works neatly and I get a nicely formated string in my db ;-)