Showing posts with label field. Show all posts
Showing posts with label field. Show all posts

Saturday, March 31, 2012

String to equation

Got a quick question that I cannot get to work at work!

I have a field in SQL server that is set as a varchar but contains data like
>50 or <2 which is actually a target percentage. I have data in another
table stored as Decimal(14,2) that is the actual percentage. I need to
extract the two and create a conditional formatting result in a datalist.

Basically it would be something like this.

Assume actualpercentage = 30
targetpercentage = >50

If actual percentagetargetpercentage = True then forecolor= "Green"
Else
forecolor = "Red"

In actual terms it would be like
If 30>50 = True then forecolor = "Green"
Else
forecolor = "Red"

Don't worry about setting the color I can get that accomplished if I could
just build the boolean expression. And trust me I know the way it is written
here seems weird but it represents what I need to test. Basically the target
percentage is anything above 50% so I need to test and see if the actual was
greater than the target. However, like I show before the target could be
anything like <2%

Thanks for any help, a function or any mechanism for that matter would be
great.

Marty UHi Mary,

Obviously the first thing to do is to parse your varchar field. You stated
that your column "contains data like >50 or <2" - you need to get more
specific than that, becuase in essence, you have stored 2 different things
in the column, and the first thing you need is to split your string into 2
pieces. The only way to do that is to know what all of the possible values
for the first (comparison operator) is, so that you can identify where
tosplit the data. Of course, this would have been much easier if you had
used 2 columns to store the 2 values; that is good database design. But once
you've identified all the possible values of the first part, you can create
a loop that loops through all of them and uses the index of the last
character to determine where to split the value. Once split into 2 values,
you need to create a loop which selects from various kinds of comparison
operators that correspond to the ones in your list of possibles, and builds
a comparison statement from one of them.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Marty Underwood" <martman100@.insightbb.com> wrote in message
news:bczYb.341294$xy6.1700666@.attbi_s02...
> Got a quick question that I cannot get to work at work!
> I have a field in SQL server that is set as a varchar but contains data
like
> >50 or <2 which is actually a target percentage. I have data in another
> table stored as Decimal(14,2) that is the actual percentage. I need to
> extract the two and create a conditional formatting result in a datalist.
> Basically it would be something like this.
> Assume actualpercentage = 30
> targetpercentage = >50
> If actual percentagetargetpercentage = True then forecolor= "Green"
> Else
> forecolor = "Red"
> In actual terms it would be like
> If 30>50 = True then forecolor = "Green"
> Else
> forecolor = "Red"
>
> Don't worry about setting the color I can get that accomplished if I could
> just build the boolean expression. And trust me I know the way it is
written
> here seems weird but it represents what I need to test. Basically the
target
> percentage is anything above 50% so I need to test and see if the actual
was
> greater than the target. However, like I show before the target could be
> anything like <2%
> Thanks for any help, a function or any mechanism for that matter would be
> great.
> Marty U
>
Hi Marty,

Look at the String.Substring() method. It's overloaded. One version takes
one parameter, which is the starting index of the substring. It reads to the
end of the string. The other takes a second parameter which is the number of
characters to get. So, assuming that your data, as you said, has only 2
single-character comparison operators, you can get the 2 values from it by
using the String.Substring method(). Example:

Dim s As String = "<123"
Dim operator As String = s.Substring(0, 1)
Dim value As Integer = Convert.ToInt32(s.Substring(1))

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Marty U" <anonymous@.discussions.microsoft.com> wrote in message
news:8ABD3BD8-4FD3-456D-8DA0-722D75DCD452@.microsoft.com...
> Thanks for the reply Kevin,
> The only two comparison operators would be the greater than, less than
operators. I would have split these into two different columns but the
customer never said they would be used for actual comparisons but just a
display mechanism. Now I don't have time to redesign the related objects
that would use the split column.
> I had an idea of creating a function that receives 3 items.
> Function ShowResult(ActualValue as Decimal, theOperator as String,
TargetValue as Decimal)
> Dim theResult as Boolean
> theResult = ActualValuetheOperatorTargetValue
> Select Case theResult
> Case "True"
> do something
> Case "False"
> do something
> End Select
> End Function
> I would use a Left(TargetValue, 1) to pass theOperator argument. Can you
look at this theory and give me an idea how I can pass these 3 items into a
function and get the desired result of whether it's true or false.
> Thanks again, I don't have time to harp on this since I have a deadline of
Friday and this is just a perk they would like to have.
Sounds good I will look into this tomorrow at work. Oh and by the way I did
split the column into two seperate columns today due to another issue I had
that was not worth the trouble. It was easier to modify six pages of code
and modify the database then to program with the data being combined in the
DB.

Marty U

"Kevin Spencer" <kevin@.takempis.com> wrote in message
news:e2rCPxk9DHA.452@.TK2MSFTNGP11.phx.gbl...
> Hi Marty,
> Look at the String.Substring() method. It's overloaded. One version takes
> one parameter, which is the starting index of the substring. It reads to
the
> end of the string. The other takes a second parameter which is the number
of
> characters to get. So, assuming that your data, as you said, has only 2
> single-character comparison operators, you can get the 2 values from it by
> using the String.Substring method(). Example:
> Dim s As String = "<123"
> Dim operator As String = s.Substring(0, 1)
> Dim value As Integer = Convert.ToInt32(s.Substring(1))
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
> "Marty U" <anonymous@.discussions.microsoft.com> wrote in message
> news:8ABD3BD8-4FD3-456D-8DA0-722D75DCD452@.microsoft.com...
> > Thanks for the reply Kevin,
> > The only two comparison operators would be the greater than, less than
> operators. I would have split these into two different columns but the
> customer never said they would be used for actual comparisons but just a
> display mechanism. Now I don't have time to redesign the related objects
> that would use the split column.
> > I had an idea of creating a function that receives 3 items.
> > Function ShowResult(ActualValue as Decimal, theOperator as String,
> TargetValue as Decimal)
> > Dim theResult as Boolean
> > theResult = ActualValuetheOperatorTargetValue
> > Select Case theResult
> > Case "True"
> > do something
> > Case "False"
> > do something
> > End Select
> > End Function
> > I would use a Left(TargetValue, 1) to pass theOperator argument. Can you
> look at this theory and give me an idea how I can pass these 3 items into
a
> function and get the desired result of whether it's true or false.
> > Thanks again, I don't have time to harp on this since I have a deadline
of
> Friday and this is just a perk they would like to have.

Wednesday, March 28, 2012

String was not recognized as a valid DateTime.

I am capturing a value from a text field and insert it into database of data type DateTime. I have used Convert.ToDateTime("stringdate") to convert it into datetime but i get the above error. But when I converted DateTime value to string and then converted it into DateTime using above method it works fine. I don't know what I need to do.

thanx for any helpIt's likely because the DateTime class can't figure out what date format the string is in. If you use DateTime.Parse that might work or if you always know what format your string is in use DateTime.ParseExact, because with that you can specify the date format.
Try:

FromDate = Convert.ToDateTime(Request.Form["FromDate"].ToString()).Date;

Where FromDate is the txtField - I had trouble with this - Seems like going around the houses a bit to me, but it works.

Pete

Saturday, March 24, 2012

Stripping the time portion from date field

Hi all
I've got a textbox on the web page that captures a date.
When an insert is done, the information captured into the database is of the format dd/mm/yyyy hh:mm:ss.
How do I strip the time portion and save only the date part into the database ?
I'm using a formview, sqldatasource controls for this web page.
I tried the following in formview iteminserting event:
Dim culture_2 As System.Globalization.CultureInfo = New CultureInfo("en-GB", True)
Dim wAcceptanceD As TextBox = CType(FormView1.Row.FindControl("dtAcceptDTextBox"), TextBox)
e.Values.Add("dtAcceptD", DateTime.Parse(wAcceptanceD.Text, culture_2, DateTimeStyles.NoCurrentDateDefault))

When I run the web page, I get the following error:

System.ArgumentException was unhandled by user code
Message="Item has already been added. Key in dictionary: 'dtAcceptD' Key being added: 'dtAcceptD'"
Source="mscorlib"
StackTrace:
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)

at System.Collections.Hashtable.Add(Object key, Object value)

at System.Collections.Specialized.OrderedDictionary.Add(Object key, Object value)

at Default3.FormView1_ItemInserting(Object sender, FormViewInsertEventArgs e) in C:\SRS\Default3.aspx.vb:line 12

at System.Web.UI.WebControls.FormView.OnItemInserting(FormViewInsertEventArgs e)

at System.Web.UI.WebControls.FormView.HandleInsert(String commandArg, Boolean causesValidation)

at System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)

at System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Can someone pleaseeeeeeeeeeeeeeeee tell me what's wrong ?
TIA.

The problem here is not the date parsing. Well, it is a problem too but not the one you're seeing.
You're using always the same key in your dictionary: "dtAcceptD". The first time it's ok, the second time, it crashes with the error you're seeing. A dictionary can have only one entry with a given key.
As for date parsing, you should specify the date format you're expecting explicitly. There is an overload of ParseExact that takes a format string.
Once you've gotten a DateTime object, it's easy to strip the time part (but why are you asking for it if you're going to throw it away? ) by constructing a new date object from the parts of the old one, something like:
new DateTime(myDate.Year, myDate.Month, myDate.Day)

Hi
Thanks for your reply.
Realised the following:
1) SQL saves the information for a datetime field with date and time information.
2) Viewing the information from the following sources have different results:
From SQL Enterprise Manager, information displayed as dd/mm/yyy.
From SQL Query Analyzer, information displayed as yyyy-mm-dd 00:00:00.
From VS (via the Database Explorer), its displayed as dd/mm/yyyy 12:00:00.
I probably dont need to do any conversion in the ItemInserting event of the formview.
Thanks for the tip on stripping the time portion of a datetime object. I'm sure it will come in handy.

Tuesday, March 13, 2012

Stuck again

Hi Guys

Here is my problem

I have a sql database with a field voucherRate(int4) that contains a figure that will be used to create a discount.

Here is my code so far


'Declare the discount string
dim strDiscount as double
'declare the total string
dim strTotal as double = 10
Sub Page_Load
'set up connection to db
Dim conNorthwind As SqlConnection
Dim dsEmployees As DataSet
dsEmployees = New Dataset

' Retrieve records from database
conNorthwind = New SqlConnection( "server=(local);uid=sa;pwd=*****;database=dbSQL" )
Dim daEmployees As SQLDataAdapter
daEmployees = New SqlDataAdapter( "Select voucherRate from vouchers", conNorthwind )
daEmployees.Fill( dsEmployees, "voucherRate" )

conNorthwind.Close()
strDiscount = daEmployees( "VoucherRate" )
End Sub

OK what I need to add but am really struggling with is this;
strDiscount = "DataSet Voucher Rate" / 100 * strTotal

What would be the correct syntax for creating this stringFirst of all...
I find your variable names very different...
I am not sure why you have "str" before all your variables...
Check out MSDN site for naming convention...

Anyhow...
here's the code to access your voucherRate field...


conNorthwind.close()

strDiscount = CType(dsEmployees.Tables(0).Rows(0).Item("voucherRate"),Integer)/100 * strTotal

But please note that your dataset might have more than one records and hence you have to loop through the dataset to calculate discount for all the records...