Saturday, March 31, 2012
Trim a DB field
<%# Mid(DataSet1.FieldValue("ListNum", Container).Trim(), 5,1) %> with a number of 675432 the result would be 3. With a number of 75432 the result would be 2. I need them always be in the 2 spot even if my field has a 7, 6, 5, or 4 digit numbers in it.
This is trimming the ListNum field in my database down to one number. When the database field is 6 numbers long it works perfectly. If the database number is 5 numbers long it selects the last number instead of the 2nd to the last number. Is there a way to get the trim command to count the numbers from the right and not the left. So the code would look something like this.
<%# Mid(DataSet1.FieldValue("ListNum", Container).Trim(), 2,1) %>
Thanks for any help.Try something like this:
Dim myString as string
myString = DataSet1.FieldValue("ListNum", Container).Trim()
myString = myString.Substring(myString.Length-2, 1)
The code is a section out of much more.. Here is the code all together.
<img src="http://pics.10026.com/?src=http://photos.neg.ctimls.com/neg/photos/small/<%# Mid(DataSet1.FieldValue("ListNum", Container).Trim(), 5,1) %>/<%# Right(DataSet1.FieldValue("ListNum", Container).Trim(), 1) %>/<%# DataSet1.FieldValue("ListNum", Container) %>a.jpg" width="100" alt="">
Because it is a piece of a code that is why I can't use something like that. Also there are many of these for one page on the site so I have to keep it as simple as possible.
Thanks
Trim fields in Text File
Hi
I have a text file with 116 fields separated by commas. How would I open the text file and trim all white spaces from each field and then close file?
Thanks
You can Use String.Split function to get array of all string with whitespace than trim that string and Use String.Join function to get comma seperated string
than store it in file
Even simpler would be to run a regex replace on your textfile after readingit.
Regex.Replace(strInput, "\s+", " ")
Trim string to fit in fixed width Table Cell [Edited by Adec, moderator]
The column of datagrid is bind to a database field.
If lenght of string is bigger than the column width i want to trim the string.
No metter if the trimmed word has sense.
I would like to know if anyone knows if there is a way to calculate
string width in pixel in connection to font size used in that particular cell.
You can check the string size and do what you need to with it in your DataGridItemCreated event handler.
that's ok for the position where check, but the problem is how measure the width in pixel of the string:-)
Your getting confused, and making this to complicated. You dont measure strings in pixels, you measure strings in Charatchers, figure out how many charatchers will fit in the column of your datagrid, and trim the string to that amount of chars, I would recomend trimming it to 3 less chars than will fit and appending on a ... so that users know it's been trimmed.
Remember, capital letters take up more space than lower case ones, and different ascii chars can take up a space and a half in some cases purley literally speaking, like the @. symbol.
I would suggest you use sql to get the substring - select substring(myfield, 1, 10) as myfield from my table. This gets the first 10 characters. To add the "..." then select substring(myfield, 1, 7)+'...' as myfield from my table
It is ok, i will follow your indication. But still look for a method like TextWidth("...") to use in application.
Thanks. Best Regards
Wednesday, March 28, 2012
Trimming text returned from db
In my App I have a datagrid with a template column. In my DB I have a field called "Bio" wich holds user biographies. The idea is that within the datagrid only a small portion of the bio is shown and visitors click onto detail pages to see photgraphs and full information. Is there a way to limit the output from the Bio field to say 100 characters
Thanks
hpYou could limit the size of what you display in your datagrid column for Bio through many ways. One of those is to limit what you fetch from the database using LEFT(Bio,100) in your SQL statement.
You could equally use a bound column. This is what MSDN has to say on that:
"A bound column (BoundColumn element) displays information from one column of a data source. You typically add one bound column to the grid for every data column you want to display. Use bound columns when you want to choose which data columns to display in the grid. Bound columns also allow you to set a variety of properties to specify features such as:
The text, font, and color of the column header and footer.
Column width.
Data format.
Whether the column is read-only (that is, whether it will display an editable control when the row is in edit mode).
For details on creating bound columns in the Visual Studio Web Forms Designer, see Adding Bound Columns to a DataGrid Web Server Control. For details on creating bound columns, see DataGrid Web Server Control."
You should be able to combine both approaches to evolve something nice.
In addition if you need the full bio from the db to use later one (perhaps they can vie the full bio via a link or something), you may want to just limit it on the front end. To do this you could use the ItemDataBound of the datagrid. If the value is stored in a control such as a lable you would use FindControl and then set the value. Something like this...
Label lblTemp = (Label)e.Item.FindControl("lblBio");
if (lblTemp != null)
lblTemp = lblTemp.Text.Substring(1, 100);
May not be completely accurate but it should get you on the right path.
Excellent, Many thanks
Trmming a Field in .NET
In Classic ASP I can take a description field and only display the first 260 characters like this: <%= Left((rsFeature.Fields.Item("description").Value), 260) %>...
Any ideas how I would accomplish this in .NET?
Thanks for any help.
try
<%= rsFeature.Fields.Item("description").Value.ToString().Substring(0,260) %>
substring will split up a string given two parameters, start index, and length.
hth,
mcm
You don′t have Left() and Right() in .NET, but you can use Substring.
string s ="Hello, world!";Console.WriteLine(s.SubString(0, 5));//prints "Hello"
So I tried this: <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>...
and get this error:
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.Parameter name: length
What if the description string does not contain 260 characters?
<%# dsFeature.FieldValue("description", Container).ToString().Substring(0,100) %>... works great.
However, if I use <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>... I get an error because some of my descriptions have less than 260 characters. Any idea how I can handle this?
Thanks!
what you should do is use the code-behind to evaluate your string expression
so
string tmpStr = "";
if(theString.length > 260){
tmpStr = theString.Substring(0,260);
}else{
tmpStr = thisString.ToString();
}
and just bind tmpStr to your field in the front end.
hth,
mcm
Got it! Thanks!
Saturday, March 24, 2012
Trouble Ticket Design (SQL + ASP.NET)
My company is a nationwide field service org. We have subcontractors across the US to service our customers things like: extending digital/analog circuits, break & fix POS system, troubleshoot in-house wiring related to telecom equipments such as csu/dsu, routers, etc..
Our trouble ticket system is currently both back and front end based in MS Access. My project is to migrate MS Access into MS SQL and write a front-end in ASP.NET (using Visual Studio .NET 2003). I've experienced with SQL and ASP only 6 months and moving to ASP.NET and handle this kind of big project is quite a challenge for me.
Anyways, my questions to all experts out there are: Can you direct me to any useful resources out there in order to help me to get a good start on this? I'm looking for more like architecture design of trouble ticket in enterprise enviroment. What sort of things do I need to watch out when coding in ASP.NET while multi-update accessing to the same ticket. I'm sure there's millions more things that I need to watch out, but can't think anymore for now.
Please help.
Any response would be greatly appreciated.
ThanhUnfortunately your question is just way too open ended. We could sit here and talk about 1,000,000 possible pitfalls for your application. I would suggest that you Google the heck out of: ASP.NET, .NET, VB.NET, C# and check out the starter kits here:
Starter Kits
Several applications that provide excellent samples of .NET code, architecture and so on. Try and get your arms around .NET a little bit and then come back with some more specific questions.
BTW- One of the starter kits is an issue tracker and might be similar to what you're faced with.
Thank you very much for quick reply, FrankWhite!
You're right, I need to get my arms around .NET much more.
Thanh
I just wanted to add that although it seems like a tall order - it can be done. I work for an Office equipment service shop. We do local service calls on equipment. I built a system ( first in Access, then ASP, and now .Net) that allows us to enter a trouble ticket, assign a tech, and track the call progress. I'm using .Net and SQL 2000. There is an Issue tracker starter kit - this should give you a good starting point to draw ideas from, along with the time tracker starter kit. Since you already hace the Access version - you should start with that. Import your tables into SQL and build the web based front end using the acces forms as a guide. Then you can tweak it to what you need.
You can do it...
HTH
Thanks for your post, UncleB.
Yeah... I was gonna code in ASP for front-end because I felt comfortable, but ASP.NET is so attractive even though I'm just .NET beginner. I'm more confident now that "can be done" with ASP.NET.
Trouble updating
Sub LoadInventory()
Connect()
Dim strQuery As String = "SELECT Type, Number FROM Inventory WHERE type = 'squares'"
Dim dbComm As New OLEDBCommand(strQuery, objConnection)
Dim reader As OLEDBDataReader = dbComm.ExecuteReader()
reader.Read()
txtAmount.Text = reader.GetString(1)
reader.Close()
Disconnect()
End Sub
Sub btnChange_Click(ByVal Sender As Object, ByVal E As EventArgs)
UpdateInventory()
LoadInventory()
End Sub
Private Sub UpdateInventory()
Dim strSQL As String = "UPDATE Inventory SET Number = " & txtAmount.Text & " WHERE Type = 'squares'"
Connect()
Dim dbComm As New OLEDBCommand(strSQL, objConnection)
dbComm.ExecuteNonQuery()
Disconnect()
End Subinstead of
Dim strSQL As String = "UPDATE Inventory SET Number = " & txtAmount.Text & " WHERE Type = 'squares'"
try this
Dim strSQL As String = "UPDATE Inventory SET Number = " & Convert.ToInt32(txtAmount.Text) & " WHERE Type = 'squares'"
Jeff
Jeff,
Thanks for your reply. That however didn't work. Could it be the way I have my access database setup?
Jeremy
Jeff,
Does it work when you try putting in a static value for txtAmount.Text such as this:
"UPDATE Inventory SET Number =1 WHERE Type='squares'"
If it does work like that, then try doing this:
Try
Dim dbComm As New OLEDBCommand(strSQL, objConnection)
dbComm.ExecuteNonQuery()
Catch dbException As Exception
Response.Write(dbException.ToString())
End Try
Tell us what the exception is. Besides, it is always good practice anyway to put database queries inside a try statement - there are just way too many things that could go wrong to not catch those errors.
Aaron
Tuesday, March 13, 2012
Trouble with time
Hi,
I'm importing some data from a text file but I have a field that looks like this
30/12/1899 15:25:00
it is time field from an access DB that when exported shows the date and time (ie date = 0) now I want just the time to put into an SQL Server.
The only thing I can think to do is treat it as a string and cut it up to get '15:25:00' and then to put that into a char field in the DB.
There has to be a better way? can anyone help?
Thanks
Hello, if you are storing your date in a string format you can do this:// Long Time
string LongTime = DateTime.Now.ToString( "T" );
// => Displays 3:47:37 PM
regards
Thanks that works nicely.
I am glad I was able to help. But watch out, if you intend to do datecomparison, that is not a good solution, its then better to storeDateTime in a DateTime field.
regards