Edit String Before Gridview Display
-
I am just starting to use ASP and am very green to its data structures, so I'm probably going about this completely wrong. On the C# side I have connected to the database and taken the info I need, but I need to strip the string of the extension before it's displayed on the asp side. Without the whole table and databinding, I would feel confident doing this, but with these two factors I'm not sure how to go about this. Is there any easy way to make my query into a table and then edit that table's entries, and set that to the datasource for binding, or am I looking at this the wrong way?
SqlConnection conn = new SqlConnection(ConnectionPath); conn.Open(); string queryClasses = "SELECT WSS\_Content.dbo.AllDocs.LeafName FROM
WSS_Content.dbo.AllDocs LEFT OUTER JOIN WSS_Content.dbo.RecycleBin ON WSS_Content.dbo.AllDocs.Id =
WSS_Content.dbo.RecycleBin.DocId WHERE (WSS_Content.dbo.RecycleBin.DocId IS NULL) AND (WSS_Content.dbo.AllDocs.Extension = 'zip')";
SqlCommand availableClasses = new SqlCommand(queryClasses, conn);
SqlDataReader reader = availableClasses.ExecuteReader();
SQLQueryClassListings.DataSource = reader;
SQLQueryClassListings.DataBind();
reader.Close();<asp:GridView ID="SQLQueryClassListings" AutoGenerateColumns="false" runat="server"
BorderWidth="1px" BackColor="White" CellPadding="5" BorderColor="DodgerBlue" HeaderStyle-
BackColor="#0147FA" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField HeaderText="Class Titles" DataField="LeafName" />
<asp:ButtonField ButtonType="Button" HeaderText="Register" Text="Register"/>
</Columns>
</asp:GridView> -
I am just starting to use ASP and am very green to its data structures, so I'm probably going about this completely wrong. On the C# side I have connected to the database and taken the info I need, but I need to strip the string of the extension before it's displayed on the asp side. Without the whole table and databinding, I would feel confident doing this, but with these two factors I'm not sure how to go about this. Is there any easy way to make my query into a table and then edit that table's entries, and set that to the datasource for binding, or am I looking at this the wrong way?
SqlConnection conn = new SqlConnection(ConnectionPath); conn.Open(); string queryClasses = "SELECT WSS\_Content.dbo.AllDocs.LeafName FROM
WSS_Content.dbo.AllDocs LEFT OUTER JOIN WSS_Content.dbo.RecycleBin ON WSS_Content.dbo.AllDocs.Id =
WSS_Content.dbo.RecycleBin.DocId WHERE (WSS_Content.dbo.RecycleBin.DocId IS NULL) AND (WSS_Content.dbo.AllDocs.Extension = 'zip')";
SqlCommand availableClasses = new SqlCommand(queryClasses, conn);
SqlDataReader reader = availableClasses.ExecuteReader();
SQLQueryClassListings.DataSource = reader;
SQLQueryClassListings.DataBind();
reader.Close();<asp:GridView ID="SQLQueryClassListings" AutoGenerateColumns="false" runat="server"
BorderWidth="1px" BackColor="White" CellPadding="5" BorderColor="DodgerBlue" HeaderStyle-
BackColor="#0147FA" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField HeaderText="Class Titles" DataField="LeafName" />
<asp:ButtonField ButtonType="Button" HeaderText="Register" Text="Register"/>
</Columns>
</asp:GridView>Ideally you would edit your data source before binding, by doing substrings inside your SQL. You can also iterate over the data source before binding. You can also write your column definition so that it calls a method and passes in the data source, so you can return whatever string you like based on the data.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Ideally you would edit your data source before binding, by doing substrings inside your SQL. You can also iterate over the data source before binding. You can also write your column definition so that it calls a method and passes in the data source, so you can return whatever string you like based on the data.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
I don't think substring will work for me because the column I am selecting has differing string lengths and from what I googled, it appears I need to know the length of the string I wish to keep. Perhaps there is a sql command I missed. I was trying to do a while loop "while(reader.Read())", but I wasn't sure exactly how to handle the reader contents. I had assumed that since it is a reader that I cannot write to its contents. Am I wrong in this assumption? I had tried to assign each iteration of reader to a string array, but that does not appear to be the path I wish to go down. My main issue is understanding what data type is coming out of reader and what format its data must be to be accepted during databinding. So, am I right about the reader and is the table idea bad? Thanks!
-
I don't think substring will work for me because the column I am selecting has differing string lengths and from what I googled, it appears I need to know the length of the string I wish to keep. Perhaps there is a sql command I missed. I was trying to do a while loop "while(reader.Read())", but I wasn't sure exactly how to handle the reader contents. I had assumed that since it is a reader that I cannot write to its contents. Am I wrong in this assumption? I had tried to assign each iteration of reader to a string array, but that does not appear to be the path I wish to go down. My main issue is understanding what data type is coming out of reader and what format its data must be to be accepted during databinding. So, am I right about the reader and is the table idea bad? Thanks!
OK, it looked to me like you wanted to drop the extension, so, the last four characters.
VengefulSakhmet wrote:
I was trying to do a while loop "while(reader.Read())"
I would fill a dataset and iterate over that.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
I am just starting to use ASP and am very green to its data structures, so I'm probably going about this completely wrong. On the C# side I have connected to the database and taken the info I need, but I need to strip the string of the extension before it's displayed on the asp side. Without the whole table and databinding, I would feel confident doing this, but with these two factors I'm not sure how to go about this. Is there any easy way to make my query into a table and then edit that table's entries, and set that to the datasource for binding, or am I looking at this the wrong way?
SqlConnection conn = new SqlConnection(ConnectionPath); conn.Open(); string queryClasses = "SELECT WSS\_Content.dbo.AllDocs.LeafName FROM
WSS_Content.dbo.AllDocs LEFT OUTER JOIN WSS_Content.dbo.RecycleBin ON WSS_Content.dbo.AllDocs.Id =
WSS_Content.dbo.RecycleBin.DocId WHERE (WSS_Content.dbo.RecycleBin.DocId IS NULL) AND (WSS_Content.dbo.AllDocs.Extension = 'zip')";
SqlCommand availableClasses = new SqlCommand(queryClasses, conn);
SqlDataReader reader = availableClasses.ExecuteReader();
SQLQueryClassListings.DataSource = reader;
SQLQueryClassListings.DataBind();
reader.Close();<asp:GridView ID="SQLQueryClassListings" AutoGenerateColumns="false" runat="server"
BorderWidth="1px" BackColor="White" CellPadding="5" BorderColor="DodgerBlue" HeaderStyle-
BackColor="#0147FA" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField HeaderText="Class Titles" DataField="LeafName" />
<asp:ButtonField ButtonType="Button" HeaderText="Register" Text="Register"/>
</Columns>
</asp:GridView>You can try something like:
string queryClasses = "select left(WSS_Content.dbo.AllDocs.LeafName, charindex('.', WSS_Content.dbo.AllDocs.LeafName, 0) - 1) from ...
Instead of charindex you might want lastIndexOf kind of thing. Please check this link: http://www.sqlservercentral.com/scripts/T-SQL+Aids/31116/[^] Hope this will help.
Anurag Gandhi. http://www.gandhisoft.com Life is a computer program and every one is the programmer of his own life.