Showing posts with label calling. Show all posts
Showing posts with label calling. Show all posts

Monday, March 26, 2012

stringbuilder question

i am calling the function "ClientInfo" from a repeater. i.e.


<itemtemplate>
<%# ClientInfo(((IDataRecord)Container.DataItem)["CLIENTCODE"].ToString())%>
</itemtemplate>

i am using stringbuilder within my function for the first time. it is working but i just wanted to make sure i am using the stringbuilder correctly. in particular, are my last 2 lines ok i.e. do i have to convert the stringbuilder back to a string in order to return the final HTML output? thanks


public string ClientInfo(string clientCode)
if (clientCode == previousclientCode)
{
System.Text.StringBuilder clientInfoStr = new System.Text.StringBuilder();
clientInfoStr.Append("<table width='100%' border='0'>");
clientInfoStr.Append("<tr class='searchresrow1'>" + SRreader.GetString(3) + "</td>");
string clientInfo = clientInfoStr.ToString();
return clientInfo;
}
else ...
In your case StringBuilder is useless, because you don't concatinate much.
I suggest you to use string.format instead of stringbuilder

For example:
<code>
string ClientInfoStr = @."
<table width='100%' border='0'>
<tr class='searchresrow1'>{0}</td>";
return string.Format(ClientInfoStr, SRreader.GetString(3));

Much simpler and faster.
HTH
sorry - i am in fact returning a long string but i cut it down to 2 lines on my post so people wouldnt get put off!

string clientInfo = clientInfoStr.ToString();

return clientInfo;

could just as well be:


return clientInfoStr.ToString();

This would save the extra String var declaration on clientInfo.

JD

Tuesday, March 13, 2012

Struggling with querystring calling in formviews

Hi,

I am having trouble getting querystrings to show up in textboxes in a formview control. I know you have to dig deeper to find the textboxes, but I thought I had done so, but it doesnt seem to work at the moment..

I am currently getting:

'Buyers_Shippingdetails' does not contain a definition for 'SellerusernameTextBox'

referring to:

Line 28: this.SellerusernameTextBox.Text = Request.QueryString["sellername"];

as an error.. my code is:

protected void FormView1_Load(object sender, EventArgs e)
{
TextBox SellerusernameTextBox = FormView1.FindControl("SellerusernameTextBox") as TextBox;

TextBox NameitemTextBox = FormView1.FindControl("NameitemTextBox") as TextBox;

TextBox prodIDTextBox = FormView1.FindControl("prodIDTextBox") as TextBox;

this.SellerusernameTextBox.Text = Request.QueryString["sellername"];
this.NameitemTextBox.Text = Request.QueryString["itemname"];
this.prodIDTextBox.Text = Request.QueryString["proID"];
}
}

Thanks if someone can help! I am stuck with the same problem on a couple of pages.

Any advice is welcome!

Cheers,

Jon

First off, remove the "this" qualifier from in fron tof your variables. These are method-level variables and don't require qualifiers. Also, I'd recommend you move your code to the FormView.DataBound event.

protected void FormView1_DataBound(object sender, EventArgs e){TextBox SellerusernameTextBox = FormView1.FindControl("SellerusernameTextBox")as TextBox;TextBox NameitemTextBox = FormView1.FindControl("NameitemTextBox")as TextBox;TextBox prodIDTextBox = FormView1.FindControl("prodIDTextBox")as TextBox;if (SellerusernameTextBox ==null) {return; }if (NameitemTextBox ==null) {return; }if (prodIDTextBox ==null) {return; }SellerusernameTextBox.Text = Request.QueryString["sellername"];NameitemTextBox.Text = Request.QueryString["itemname"];prodIDTextBox.Text = Request.QueryString["proID"];}

Just the trick.

Thanks alot, as always.

Jon