Showing posts with label display. Show all posts
Showing posts with label display. Show all posts

Saturday, March 31, 2012

Trim String

Hi

I want to trim the end of a string placed on my page.

I want to make it so that even if the text is 100 char long it will only display the first 15 characters.

Is this correct, i cant seem to make it work.

Regards
Ant

<%#Container.DataItem("OutboundCarrierName").ToString.Trim("15") %>

use Substring function in place of this:

<%#Container.DataItem("OutboundCarrierName").ToString().Substring(0,15) %>

thanks


Perfect, that works.

Wednesday, March 28, 2012

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!

Trouble connecting to an SQL database in C#

Hi guys,

I'm using Visual Web Developer and I've made a GridView and I want to learn how to display information in it (or any other databound control) rather than doing it through the asp source code. I'm using the following code so far, and although I've probably made some errors in it I think there is a problem connecting to the database remotely as I get the following error on the "myConnection.Open()" line.

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I tried following the instructions here http://www.datamasker.com/SSE2005_NetworkCfg.htm but didn't have much luck as I don't seem to have the interface shown in the screen prints.

My code so far is

SqlConnection myConnection = new SqlConnection("Server=local;database=WroxUnited");
SqlCommand myCommand = new SqlCommand("SELECT * FROM [Fixtures]", myConnection);

myConnection.Open();

GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();

myConnection.Close();

Thanks in advance.You specified a server (Local) and a database (WroxUnited) ... just so that we're on the same page, that isn't a remote DB... it's local on your PC.

But the problem is probably because you didn't specify an authentication method... do you intend to use integrated security (using WIndows Authentication) or to use SQL Server Authentication? IF you want integrated securtity then you need to add "Integrated Security=SSPI;" to the connection string otherwise add "Integrated Security=False;User ID=XXXXXX;Password=YYYYYYYY;"

-tg
You can make use of the idea in this code:

SqlConnection sc1 = new SqlConnectionConfigurationManager.ConnectionStrings ["CompanyConnectionString"].ToString ());
SqlDataAdapter sa1 = new SqlDataAdapter();
sc1.Open();
string c = "Select Name , Postion_Name from Employee join Postion on Postion_Num=Postion_ID";
SqlCommand sqc1 = new SqlCommand(c,sc1);
DataSet ds = new DataSet();
sa1.SelectCommand = sqc1;
sa1.Fill(ds);
GridView1.DataSource = ds;
What's annoying is there seems to be so many different ways to go about it, and as I'm just beginning to learn .NET it's all a bit confusing. I'm sure I'll get it eventually but I'm worried I may have already got it but something's not set up correctly to work. However if I do it through ASP source code using a ConnectionString=<%$ConnectionStrings=WroxUnited%> for example then it does work so I suppose it's unlikely to be a problem with the SQL Server set-up.

Just to test that it does actually work would someone be kind enough to post here the smallest amount of code necessary to display data from a database called WroxUnited.mdf stored in the C:, in a gridview called GridView1? Or if it's easier to use another data control then let me know and I'll do it that way.

Thanks for all the help!

Monday, March 26, 2012

Trouble getting jpg to display in image control

Hi,

My company is changing its logo and I am having trouble getting my
existing image controls in asp.net apps to read the new jpg. I do not
need to programmatically change them, as they are static, but when
using the new jpegs I only get the little red x. I have added the new
jpg to the project, I can see it right there when I go to set the
image url of the control, and when I double click from the solution
explorer it displays properly in Photo Editor but no luck in my
control. Creating new controls and changing existing ones produces
the same result. I am able to use a new gif, but I am unhappy with
the image quality.

Any suggestions would be great.You'll want to make sure that the URL isn't pointing to your local file
system (file:) rather than a regular IIS url.

Also, is this inside the firewall? Are you using the same filename for the
new image?

Some intranets have very aggressive caching. You might defeat it by
changing the name of the .jpg if you haven't already tried that. Or ask the
security admin to dump the cache.

Just tossing out some ideas....

"Brian Hanson" <bdhanson@.mcdermott.com> wrote in message
news:169b400f.0312010828.2ad4bcb9@.posting.google.c om...
> Hi,
> My company is changing its logo and I am having trouble getting my
> existing image controls in asp.net apps to read the new jpg. I do not
> need to programmatically change them, as they are static, but when
> using the new jpegs I only get the little red x. I have added the new
> jpg to the project, I can see it right there when I go to set the
> image url of the control, and when I double click from the solution
> explorer it displays properly in Photo Editor but no luck in my
> control. Creating new controls and changing existing ones produces
> the same result. I am able to use a new gif, but I am unhappy with
> the image quality.
> Any suggestions would be great.

Saturday, March 24, 2012

Trouble with ArrayList and GridView

Hi all,

I am trying to display somedata from a postgres database, and display it using an asp.net page, using a GridView. The basic thing I am doing is, getting data from the database, storing it in an ArrayList, and passing the ArrayList to the DataSource for the GridView.

The problem is the data that I need to display is 3 dimensional. My questions are:

How can I create an ArrayList that can hold 3 columns for each row? How do I populate such an ArrayList? How can I pass this data to the GridView? How can I getHyperlinks on all the values displayed in the first column?

I am a beginner atasp.net. Any help will be appreciated.

Thanks
George

Hey,

The preferred data object for this is a DataTable. Though I don't know if there is ADO.NET support for a postgres database (I don't know anything about that database system), you could still make use of a DataTable, as that works well with the GridView.

The other alternative is that you get the data back from the postgres database and populate it into a business object that contains the three properties of information you are returning. An arraylist can be a list of business objects, which each object has the three properties.


Ok, I tried using the DataTable, so now I am retrieving data from my table, populating the data table and displaying it. This works fine.

But what I need is a query that retrieves data in a 'for' loop, since I need to pass different variables to the query everytime. So I pass a variable, run the query, store the result somewhere(an ArrayList?), then pass the next variable, run the query, append the result to my storage and so on...

I am not sure if we can append data to a DataTable(if we can, I would like to know how). And incase ArrayList is the best option available, how would I be able to create an ArrayList that would contain 3 columns? How can this be displayed in a GridView?

Thanks for the reply. Hoping to get around this problem soon.

George


to add hyper link all you have to do is right click over the grid veiw and select Show Smart Tag >>>

Then in the Smart Tag of the GridVeiw click the Edit columns link

A field wizard will pop, in that make sureAuto-generate field option is checked

In the Available field sectionselect the HyperLinkField and click the Add button

Then Select the HyperLinkField in the selected field section

Inside the HyperLinkField Properties section :-

? In appearrance section make the text to go

? In data section make the DataNagivationUrlFormatString to topic.aspx?TopicId={0}

(Here topic.aspx is the page you wan to redirect and TopicId is the QueryString Variable)

? In data section make the DataNavigationUrlField toTopicId

(This actually specify the value of {0})


At the first, DataTable is the best option for you!

Yes, you can add a DataRow to a DataTable:

// Define a DataTable

DataTable tbl =newDataTable();

// Add your Columns to DataTable

tbl.Columns.Add("Col1");

tbl.Columns.Add("Col2");

// Create a DataRow for this table

DataRow row = tbl.NewRow();

// Set you row values

row[0] ="c1";

row["Col2"] = 25;

// Add row to table

tbl.Rows.Add(row);

Also, you can use Rows of your table in "for" loop:

foreach (DataRow rwin tbl.Rows)

{

Response.Write(rw["Col1"]);

}

or

for (int i = 0; i < tbl.Rows.Count; i++)

{

Response.Write(tbl.Rows[i]["Col2"]);

}


Hello,

Yes, the code above can be used to iterate through a data table. You can also use a DataView to filter the data by using the RowFilter property:

http://msdn2.microsoft.com/en-us/library/system.data.dataview(VS.71).aspx
http://davidhayden.com/blog/dave/archive/2006/02/11/2798.aspx