예제 #1
0
        public Boolean saveClick()
        {
            if (this._isqlsave.Equals(String.Empty) || this._isqlverifysave.Equals(String.Empty))
            {
                return false;
            }
            else
            {
                String fields = " ";

                for (int i = 0; i < this._fields.Count; i++)
                {

                    // se é campo editável
                    if (this._fields[i].editable())
                    {

                        // verifica se é nulo
                        if (this._fields[i].notnull())
                        {
                            if (this._fields[i].fieldtype() == FieldType.TextBox)
                            {
                                if (((TextBox)this._fields[i].pageelement()).Text.Equals(String.Empty))
                                {
                                    alert("Preencha o campo " + this._fields[i].desc() + "!");
                                    return false;
                                }
                            }
                            else if (this._fields[i].fieldtype() == FieldType.SelectBox)
                            {
                                if (((DropDownList)_fields[i].pageelement()).SelectedValue.Equals(String.Empty))
                                {
                                    alert("Preencha o campo " + this._fields[i].desc() + "!");
                                    return false;
                                }
                            }
                        }

                        // verifica data
                        if (this._fields[i].sqltype() == SQLType.Date && this._fields[i].fieldtype() == FieldType.TextBox)
                        {
                            try
                            {
                                String value = ((TextBox)this._fields[i].pageelement()).Text;
                                DateTime dt = new DateTime(Convert.ToInt32(value.Substring(6, 4)),
                                    Convert.ToInt32(value.Substring(3, 2)), Convert.ToInt32(value.Substring(0, 2)));
                            }
                            catch (Exception ex)
                            {
                                alert("Digite uma data válida (DD/MM/AAAA) para o campo " + this._fields[i].desc() + "!");
                                return false;
                            }
                        }

                        // verifica campo numérico
                        if (this._fields[i].sqltype() == SQLType.Numeric && this._fields[i].fieldtype() == FieldType.TextBox && (i != _identity))
                        {

                            try
                            {
                                String value = ((TextBox)this._fields[i].pageelement()).Text;
                                if (!value.Equals(String.Empty) && this._fields[i].notnull())
                                {
                                    value = value.Replace(",", ".");

                                    Double db = Convert.ToDouble(value);
                                }
                            }
                            catch
                            {
                                alert("Digite um valor numérico válido para o campo " + this._fields[i].desc() + "!");
                                return false;
                            }
                        }

                        String aux;

                        if (this._fields[i].fieldtype() == FieldType.TextBox)
                        {
                            aux = ((TextBox)this._fields[i].pageelement()).Text;
                        }
                        else if (this._fields[i].fieldtype() == FieldType.SelectBox)
                        {
                            aux = ((DropDownList)_fields[i].pageelement()).SelectedValue;
                        }
                        else if (this._fields[i].fieldtype() == FieldType.CheckBox)
                        {
                            aux = ((CheckBox)_fields[i].pageelement()).Checked ? "1" : "0";
                        }
                        else if (this._fields[i].fieldtype() == FieldType.Calendar)
                        {
                            aux = ((Calendar)_fields[i].pageelement()).SelectedDate.Year.ToString() + "-" +
                                ((Calendar)_fields[i].pageelement()).SelectedDate.Month.ToString() + "-" +
                                ((Calendar)_fields[i].pageelement()).SelectedDate.Day.ToString();
                        }
                        else
                        {
                            aux = String.Empty;
                        }

                        fields += ((aux.Equals(String.Empty)&& i != _identity) ? ("NULL,") : ("'" + aux.Replace("'", "''") + "',"));
                    }

                }

                fields = fields.Substring(0, fields.Length - 1);

                CDataBase cdb = new CDataBase();
                CQuery query;

                if (!this._isqlverifysave.Equals("-1"))
                {
                    query = cdb.select(this._isqlverifysave + fields, "P_CHR_MSG_ERRO");
                }
                else
                {
                    query = new CQuery();
                    CRow row = new CRow();
                    row.addField("", "");
                    query.addRow(row);

                }

                if (query.Count() == 0)
                {
                    alert("Houve um erro ao salvar o registro!");
                    this.setPageStatus(StatusPage.OpenUnload);
                    return false;
                }
                else
                {
                    if (!query.Field(0, 0).Equals(String.Empty))
                    {
                        alert("Não foi possível salvar o registro!\\n" + query.Field(0, 0));
                        clearFields();
                        this.setPageStatus(StatusPage.OpenUnload);
                        return false;
                    }
                    else
                    {
                        try
                        {
                            cdb.execute(this._isqlsave + fields);
                            this.setIdentity(cdb);
                            this.setPageStatus(StatusPage.AfterSaveClick);
                        }
                        catch (Exception ex)
                        {
                            alert("Erro ao salvar o registro!\\n" + ex.Message);
                            this.setPageStatus(StatusPage.OpenUnload);
                        }
                    }
                }

                return true;
            }
        }
예제 #2
0
        // executa uma consulta no banco e retorna um objeto CQuery
        public CQuery select(String isql, String sfields)
        {
            // determina vetor de campos
            String[] fields = sfields.Split(';');

            // cria objeto query
            CQuery query = new CQuery();

            try
            {

                // executa a instrução sem fechar a conexão
                this.execute(isql, false);

                // monta o objeto query
                this._rstRecord = this._cmdCommand.ExecuteReader();

                while (_rstRecord.Read())
                {
                    CRow row = new CRow();

                    for (int i = 0; i < fields.Length; i++)
                    {
                        try
                        {
                            row.addField(fields[i], _rstRecord[fields[i]].ToString());
                        }
                        catch (Exception ex)
                        {
                            row.addField(fields[i], "");
                        }
                    }

                    query.addRow(row);

                }
            }
            catch (Exception ex)
            {
                query = null;
            }
            finally
            {
                // fecha a conexão
                this.closeConnection();
            }

            // retorna a consulta
            return query;
        }