Thursday, April 30, 2015

else if statement allows false

Hello

I have the following code where i,m trying to see if a CustId exist or not and if it does return data from the database to the textboxes but if not it returns a message in the lblMessage.

But what is happing in my else if statement is allows false so it throws the lblMessage "can not find Customer Id" or an Exception is i change the code to

else if (CustID ==0){
//then code
}

 protected void Button1_Click(object sender, EventArgs e)
        {
            
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection1"].ConnectionString);
            int CustID = 0;
            SqlCommand command = new SqlCommand();

            command.Connection = conn;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "GetCustmer";
            command.Connection.Open();

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@ID";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            param.Value = txtCustID.Text;
            command.Parameters.Add(param);
            int ID = table.Rows.Count;
            if (string.IsNullOrWhiteSpace(txtCustID.Text))
            {
                //throw new Exception("Id not provided.");
                lblMessage.Text = "You did not enter a Customer Number";

            }
            else if(CustID !=0)
            {
                adapter.SelectCommand = command;
                adapter.Fill(table);

                //txtCustID.Text = table.Rows[0].Field<string>("CustID");
                txtFirstname.Text = table.Rows[0].Field<string>("Firstname");
                txtFirstname.DataBind();
                txtSurname.Text = table.Rows[0].Field<string>("Surname");
                txtSurname.DataBind();
            }
            else {
                lblMessage.Text = "Could not find customer number";
            }
            
        }

Thanks for your help

else if statement allows false

Hi,

custID will always be 0, just as being initialized, you never set it to an other value.

Like:

Int32.TryParse(txtCustID.Text, out CustID);
 

Regards,

  Thorsten


System.NullReferenceException was unhandled ?

here
dr = cmd.ExecuteReader();

How do I convert a SQL Server Date MM/DD/CCYY to an Oracle Date formatted as NUMBER(8,0)

So I have to build dynamic T-SQL because of a date parameter that will be provided. The Date Parameter will be provided in SSRS in normal MM/DD/CCYY format. So how do I then convert that date to my Oracle format NUMERIC(8,0) CCYYMMDD?

I tried this...

SET           @SQLQuery       =       @SQLQuery       +   'AND    MEMBER_SPAN.YMDEFF                                              <=                   '''''   +   CAST(@AsOfDate  AS      VARCHAR)        +   '''''   '       +   @NewLineChar;
SET             @SQLQuery       =       @SQLQuery       +   'AND    MEMBER_SPAN.YMDEFF                                              >=                   '''''   +   CAST(@AsOfDate  AS      VARCHAR)        +   '''''   '       +   @NewLineChar;

but that put it in the format of...

AND

MEMBER_SPAN.YMDEFF<=''2015-04-01''

AND

MEMBER_SPAN.YMDEFF>=''2015-04-01''

which is close...I think I just need to lose the "-"

Thanks for your review and am hopeful for a reply.

How do I convert a SQL Server Date MM/DD/CCYY to an Oracle Date formatted as NUMBER(8,0)

Please check this?

SELECT replace(convert(VARCHAR, getdate(), 111) , '/', '-') 


Please click "Mark as Answer" if the post solves your problem - Thanks

How do I convert a SQL Server Date MM/DD/CCYY to an Oracle Date formatted as NUMBER(8,0)

Try ISO style 112 with CONVERT:


--T-SQL
select CONVERT(Char(10),Cast('2015-04-01' as date),112) , CONVERT(Char(10),current_timestamp,112)

How do I convert a SQL Server Date MM/DD/CCYY to an Oracle Date formatted as NUMBER(8,0)

Hi ITBobbyp,

For SQL Server 2012 and above, you can use FORMAT.

SELECT FORMAT(CAST('04/01/2015' AS DATE),'yyyyMMdd') AS DT

/*
DT
20150401
*/

If you have any question, feel free to let me know.

Eric Zhang
TechNet Community Support