The goal is to build a SQL Query from lines in a multiline text box. Where the result will look like;
SELECT * FROM TEST WHERE ITEM IN('Line 1','Line 2','Line 3','...');
But for some reason I keep getting the following with String.Join
SELECT * FROM TEST WHERE ITEM IN('Line 1','','Line 2','','Line 3','','Line 4','','Line 5');
Here is my .aspx file
<asp:TextBox ID="TxtTest" runat="server" Height="250px" TextMode="MultiLine"
Width="134px" Wrap="False" MaxLength="7"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="ButtonTest" runat="server" Text="Button" Width="128px"
onclick="ButtonTest_Click" />
</div>
</form>
And here is my Code behind
protected void ButtonTest_Click(object sender, EventArgs e)
{
StringBuilder test = new StringBuilder();
test.Append("Select * FROM TEST WHERE ITEM IN ('");
string s = TxtTest.Text;
// Split string on newlines
string[] lines = s.Split(Environment.NewLine.ToCharArray());
//recombine the array elements with ','
string result = String.Join("','", lines);
test.Append(result);
test.Append("');"); //closes SQL statement
Label1.Text = test.ToString();
}
Does anyone know why it is doing the ',' twice between the parts during runtime and how to correct it? I also tried it using StringBuilder with Append but it was giving me a similar issue.
TIA
This post has been edited by kmc5117: 08 February 2011 - 02:08 PM

New Topic/Question
Reply




MultiQuote





|