Howdy,
I need to compine two columns (LastName, Firstname) with the comma.
The only problem I have is the Firstname could be blank and in that
case I don't want the "," appended to the last name.
This data is coming out of a Sql Server data base. I am currently
using a DataReader and simply binding it to a data grid to display the
information.
I realize that I could use a dataset/datatable instead of a reader and
add a new column, read thorugh the datatable and build the new column.
Then delete or hide the two original columns. But is that the
best(most effecient) way to do it?
Thanks
dbl 10 1986
You will need a template to do this. I assume you're using a DataGrid?
<asp:DataGrid Runat=server ID=_grid>
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# GetFirstLastNam e(Container.Dat aItem) %>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
And then some code like this:
<script runat="server">
string GetFirstLastNam e(object row)
{
string fname = DataBinder.Eval (row, "FirstName" ) as string;
string lname = DataBinder.Eval (row, "LastName") as string;
if (fname == null || fname.Trim().Le ngth == 0)
{
return lname;
}
return String.Format(" {0}, {1}", lname, fname);
}
</script>
Fill in your own logic (and testing too! -- I didn't run this code). You
get the idea, though.
-Brock
DevelopMentor http://staff.develop.com/ballen Howdy,
I need to compine two columns (LastName, Firstname) with the comma. The only problem I have is the Firstname could be blank and in that case I don't want the "," appended to the last name.
This data is coming out of a Sql Server data base. I am currently using a DataReader and simply binding it to a data grid to display the information.
I realize that I could use a dataset/datatable instead of a reader and add a new column, read thorugh the datatable and build the new column. Then delete or hide the two original columns. But is that the best(most effecient) way to do it?
Thanks
dbl
Just my $0.02...I think Brock's suggestion is the best, unless this is
something that you'll be doing often. You mentioned a dataset and obviously
taking advantage of the capability to cache them might be beneficial (merge
the columns once into a new column and be done with it). Brock's way
obviously does this work each time....since you didn't provide any broader
scope about your usage, it's impossible to guess at which method might be
best...but atleast wanted to point out the differences (even though they are
somewhat obvious).
Karl
--
MY ASP.Net tutorials http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying) http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:32******** **************@ msnews.microsof t.com... You will need a template to do this. I assume you're using a DataGrid?
<asp:DataGrid Runat=server ID=_grid> <Columns> <asp:TemplateCo lumn> <ItemTemplate > <%# GetFirstLastNam e(Container.Dat aItem) %> </ItemTemplate> </asp:TemplateCol umn> </Columns> </asp:DataGrid>
And then some code like this:
<script runat="server"> string GetFirstLastNam e(object row) { string fname = DataBinder.Eval (row, "FirstName" ) as string; string lname = DataBinder.Eval (row, "LastName") as string; if (fname == null || fname.Trim().Le ngth == 0) { return lname; } return String.Format(" {0}, {1}", lname, fname); } </script>
Fill in your own logic (and testing too! -- I didn't run this code). You get the idea, though.
-Brock DevelopMentor http://staff.develop.com/ballen Howdy,
I need to compine two columns (LastName, Firstname) with the comma. The only problem I have is the Firstname could be blank and in that case I don't want the "," appended to the last name.
This data is coming out of a Sql Server data base. I am currently using a DataReader and simply binding it to a data grid to display the information.
I realize that I could use a dataset/datatable instead of a reader and add a new column, read thorugh the datatable and build the new column. Then delete or hide the two original columns. But is that the best(most effecient) way to do it?
Thanks
dbl
Brock,
I have it working sort of ... I get the columns I want but I also get
all the other columns in the DataTable. Can you tell me what Im not
doing or doing wrong? Here is my code that is on a button event:
private void cmdSubmit_Click (object sender, System.EventArg s e)
{
// string sConnection = "Integrated Security=SSPI;P ersist
Security Info=False;data base=LSICountyW eb;server=REVEL ATIONS;Connect
Timeout=30";
string sConnection = "user
id=username;pas sword=something ;database=LSICo untyWeb;server= REVELATIONS;Con nect
Timeout=30";
SqlConnection myConn = new SqlConnection(s Connection);
SqlCommand myCommand;
SqlDataAdapter myDA = new SqlDataAdapter( );
DataSet myDS;
string sName = "";
string sSql = "";
sName = txtName.Text;
sSql = "Select top 100 [Name], [GivenName], Count(*) as
Matches From NCLand Where [Name] Like '" + sName + "%' Group By [Name],
[GivenName] Order by [Name], [GivenName]";
if (sName.Length > 0)
{
myCommand = new SqlCommand(sSql , myConn);
myDA = new SqlDataAdapter( );
myDA.SelectComm and = myCommand;
myConn.Open();
myDS = new DataSet();
myDA.Fill(myDS, "SearchResults" );
grdMatching.Dat aSource =
myDS.Tables["SearchResu lts"].DefaultView;
grdMatching.Dat aBind();
grdMatching.Vis ible = true;
}
}
public string GetName(object row)
{
string sGivenName = DataBinder.Eval (row, "GivenName" ) as
string;
string sName = DataBinder.Eval (row, "Name") as string;
if (0 != sGivenName.Leng th)
{
sName += "," + sGivenName;
}
return sName;
}
Here is the HTML code:
<asp:DataGrid id="grdMatching " style="Z-INDEX: 105; LEFT:
176px; POSITION: absolute; TOP: 176px"
runat="server" Width="616px" Visible="False" >
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# GetName(Contain er.DataItem) %>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn>
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,
"Matches") %>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid></form>
you could have sql do the work also
sSql = @"Select top 100
[Name],
[GivenName],
case when GivenName <> '' then Name + ', ' + GivenName
else Name
ens as FullName,
Count(*) as Matches
From NCLand Where [Name] Like '" + sName + "%' Group By [Name],
[GivenName] Order by [Name], [GivenName]";
note: your sql allows sql injection which is a high secuirty risk.
-- bruce (sqlwork.com)
"DBLWizard" <ib*********@ya hoo.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. . Brock,
I have it working sort of ... I get the columns I want but I also get all the other columns in the DataTable. Can you tell me what Im not doing or doing wrong? Here is my code that is on a button event:
private void cmdSubmit_Click (object sender, System.EventArg s e) { // string sConnection = "Integrated Security=SSPI;P ersist Security Info=False;data base=LSICountyW eb;server=REVEL ATIONS;Connect Timeout=30"; string sConnection = "user id=username;pas sword=something ;database=LSICo untyWeb;server= REVELATIONS;Con nect Timeout=30"; SqlConnection myConn = new SqlConnection(s Connection); SqlCommand myCommand; SqlDataAdapter myDA = new SqlDataAdapter( ); DataSet myDS; string sName = ""; string sSql = "";
sName = txtName.Text; sSql = "Select top 100 [Name], [GivenName], Count(*) as Matches From NCLand Where [Name] Like '" + sName + "%' Group By [Name], [GivenName] Order by [Name], [GivenName]";
if (sName.Length > 0) { myCommand = new SqlCommand(sSql , myConn); myDA = new SqlDataAdapter( ); myDA.SelectComm and = myCommand; myConn.Open(); myDS = new DataSet(); myDA.Fill(myDS, "SearchResults" ); grdMatching.Dat aSource = myDS.Tables["SearchResu lts"].DefaultView; grdMatching.Dat aBind(); grdMatching.Vis ible = true; } }
public string GetName(object row) { string sGivenName = DataBinder.Eval (row, "GivenName" ) as string; string sName = DataBinder.Eval (row, "Name") as string;
if (0 != sGivenName.Leng th) { sName += "," + sGivenName; }
return sName; }
Here is the HTML code:
<asp:DataGrid id="grdMatching " style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 176px" runat="server" Width="616px" Visible="False" > <Columns> <asp:TemplateCo lumn> <ItemTemplate > <%# GetName(Contain er.DataItem) %> </ItemTemplate> </asp:TemplateCol umn> <asp:TemplateCo lumn> <ItemTemplate > <%# DataBinder.Eval (Container.Data Item, "Matches") %> </ItemTemplate> </asp:TemplateCol umn> </Columns> </asp:DataGrid></form>
Hi,
You can change your SQL query to:
Select top 100 [Name], [GivenName], [GivenName] + ISNULL(',' + [NAME],
'') [FullName], Count(*) as Matches
From NCLand
Where [Name] Like '" + sName + "%'
Group By [Name],[GivenName] Order by [Name], [GivenName]";
"DBLWizard" <ib*********@ya hoo.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com... Howdy,
I need to compine two columns (LastName, Firstname) with the comma. The only problem I have is the Firstname could be blank and in that case I don't want the "," appended to the last name.
This data is coming out of a Sql Server data base. I am currently using a DataReader and simply binding it to a data grid to display the information.
I realize that I could use a dataset/datatable instead of a reader and add a new column, read thorugh the datatable and build the new column. Then delete or hide the two original columns. But is that the best(most effecient) way to do it?
Thanks
dbl
Tell the DataGrid to not generate all the columns automatically:
<asp:DataGrid AutoGenerateCol umns=false>
But then you'll have to tell it which columns to show:
<Columns>
<asp:BoundColum n HeaderText="MyC olumn" DataField="DBCo lumnName" />
</Columns>
-Brock
DevelopMentor http://staff.develop.com/ballen Brock,
I have it working sort of ... I get the columns I want but I also get all the other columns in the DataTable. Can you tell me what Im not doing or doing wrong? Here is my code that is on a button event:
private void cmdSubmit_Click (object sender, System.EventArg s e) { // string sConnection = "Integrated Security=SSPI;P ersist Security Info=False;data base=LSICountyW eb;server=REVEL ATIONS;Connect Timeout=30"; string sConnection = "user id=username;pas sword=something ;database=LSICo untyWeb;server= REVELATION S;Connect Timeout=30"; SqlConnection myConn = new SqlConnection(s Connection); SqlCommand myCommand; SqlDataAdapter myDA = new SqlDataAdapter( ); DataSet myDS; string sName = ""; string sSql = ""; sName = txtName.Text; sSql = "Select top 100 [Name], [GivenName], Count(*) as Matches From NCLand Where [Name] Like '" + sName + "%' Group By [Name], [GivenName] Order by [Name], [GivenName]";
if (sName.Length > 0) { myCommand = new SqlCommand(sSql , myConn); myDA = new SqlDataAdapter( ); myDA.SelectComm and = myCommand; myConn.Open(); myDS = new DataSet(); myDA.Fill(myDS, "SearchResults" ); grdMatching.Dat aSource = myDS.Tables["SearchResu lts"].DefaultView; grdMatching.Dat aBind(); grdMatching.Vis ible = true; } } public string GetName(object row) { string sGivenName = DataBinder.Eval (row, "GivenName" ) as string; string sName = DataBinder.Eval (row, "Name") as string; if (0 != sGivenName.Leng th) { sName += "," + sGivenName; } return sName; } Here is the HTML code:
<asp:DataGrid id="grdMatching " style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 176px" runat="server" Width="616px" Visible="False" > <Columns> <asp:TemplateCo lumn> <ItemTemplate > <%# GetName(Contain er.DataItem) %> </ItemTemplate> </asp:TemplateCol umn> <asp:TemplateCo lumn> <ItemTemplate > <%# DataBinder.Eval (Container.Data Item, "Matches") %> </ItemTemplate> </asp:TemplateCol umn> </Columns> </asp:DataGrid></form>
Bruce,
Where am I vulnerable here to sql injection? I was not too worried
about it in this case becuase the account that is used for this is read
only but I would like to know better how to handle sql injection and I
didnt think I was open to that in this query.
Thanks
dbl
in the search name field on your form type:
a'' delete NCLand select * from NCLand where name=''a
-- bruce (sqlwork.com)
"DBLWizard" <ib*********@ya hoo.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . Bruce,
Where am I vulnerable here to sql injection? I was not too worried about it in this case becuase the account that is used for this is read only but I would like to know better how to handle sql injection and I didnt think I was open to that in this query.
Thanks
dbl
I agree. Doing this work in the query (preferably a stored procedure) is
the most efficient solution in most cases.
--
I hope this helps,
Steve C. Orr, MCSD, MVP http://SteveOrr.net
"The Developer" <pi*******@gmai l.com> wrote in message
news:e%******** ********@TK2MSF TNGP10.phx.gbl. .. Hi, You can change your SQL query to:
Select top 100 [Name], [GivenName], [GivenName] + ISNULL(',' + [NAME], '') [FullName], Count(*) as Matches From NCLand Where [Name] Like '" + sName + "%' Group By [Name],[GivenName] Order by [Name], [GivenName]";
"DBLWizard" <ib*********@ya hoo.com> wrote in message news:11******** *************@l 41g2000cwc.goog legroups.com... Howdy,
I need to compine two columns (LastName, Firstname) with the comma. The only problem I have is the Firstname could be blank and in that case I don't want the "," appended to the last name.
This data is coming out of a Sql Server data base. I am currently using a DataReader and simply binding it to a data grid to display the information.
I realize that I could use a dataset/datatable instead of a reader and add a new column, read thorugh the datatable and build the new column. Then delete or hide the two original columns. But is that the best(most effecient) way to do it?
Thanks
dbl
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lachlan Hunt |
last post by:
Hi,
I have recently downloaded and experemented with IBM HPR 3.0, and
Opera 8 with text-to-speech, and have come to realise some fairly
annoying issues regarding punctuation marks.
I've found, that when a punctuation mark occurs directly after an
element, both HPR and Opera 8 will read the punctuation mark.
For example, the following:
<p><abbr title="...">HTML</abbr> is an application of
|
by: Lars E. |
last post by:
Hi all.
I am trying to combine data from 2 tables in one datagridview.
I have tables: "Customer" and "CustomerContact".
I want to display Customer information (displaying all fields in customer and some fields from customercontact) in a datagridview. I am not using sql so innerjoin is not possible....
The relation between the tables are: Customer.c-custno = CustomerContact.cc-custno.
|
by: tshad |
last post by:
I have about 10 functions that are identical except for one variable name
that I am using in my "for" loop:
for (ktr1=1;ktr1<=nhDataBean.projectCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.shiftCodeList.GetUpperBound(0);ktr1++)
for (ktr1=1;ktr1<=nhDataBean.statusCodeList.GetUpperBound(0);ktr1++)
How can I change the "nhDataBean.xxx.GetUpperBound(0)" so that I can use
only one function?
|
by: zwieback89 |
last post by:
Hi,
I am still not able to get this working in a simple page. Please help me. I am badly stuck....
I am trying to view the contents of the Beverage Category. I have built it like a hierarchy in the form of styled <UL> tags.
Please assist me in combining the 2 pages into one page. I would extremely grateful.
My code is:
|
by: dayhill |
last post by:
I am trying to combine three columns (month, day, year) to make one date (month-day-year).
Here is the intital inforation when I hit the SQL button in Microsoft Query
UtilityMaster.mDateAutoInst1, UtilityMaster.dDateAutoInst1, UtilityMaster.yDateAutoInst1
Here is what I modified it to:
UtilityMaster.mDateAutoInst1+'-'+UtilityMaster.dDateAutoInst1+'-'+UtilityMaster.yDateAutoInst1
When I hit OK, I get the following error: ...
| |
by: denny1824 |
last post by:
Hi everyone,
Here is my problem. I have a Table Variable (I could easily turn it into a Temp Table instead) that will sometimes have rows that are identical except for one specific column. The column is of nvarchar. When the rest of the columns for those rows are identical, I want to combine all of the values of that column into a single nvarchar with a delimiter in between each value. Then combine the rows where all of the other columns are...
|
by: bluereign |
last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script below. My problem is if I don’t use the WHERE clause in my script below, the script can query up to 3 records with the same “LoanNo” due to the fact that there can be up to 3 “TypeCodes” in each Table (Borrower, Co-Borrower 1 and Co-Borrower 2...
|
by: vineetbindal |
last post by:
Hi All.
I have two Columns column1 and column2. i have to run a query with some value from colum1 depending on it will select result from coloumn2 and if that result is present in coloumn 1 it will again select something from coloum2 again if that new result is present in coloum 1 it will select something from colum2 . i have to combine all this in one column without repeating values.
something like.
Column1 Column2 Column3
2100 ...
|
by: stateemk |
last post by:
I have a db with a table that has many columns of info for various entities. In this table, there are three columns called, entity name, second entity name and third entity name. I need to combine all three columns into one drop box so a search can be done. Once the correct entity name is found, I need to be able to select it and bring up a form with all the data for that entity. For example, say there is an entity that has a name change so...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |