Guys i have a query that is returning a number of tax codes.(depending on document number chosen)
伙计们我有一个返回多个税码的查询。(取决于所选的文件编号)
//call method DoQuery and assign returned result to DataReader
DataReader = DoQuery("select oa.[OtherAmountCode] as 'Tax code'
FROM [Biatss_PC_SRV].[Pax].[SalesDocumentOtherAmount] as oa
INNER JOIN [Biatss_PC_SRV].[Pax].[SalesRelatedDocumentInformation]
as rd on oa.RelatedDocumentGuid = rd.RelatedDocumentGuid
INNER JOIN [Biatss_PC_SRV].[Pax].[SalesDocumentHeader] as h
on rd.HdrGuid = h.HdrGuid
where h.DocumentNumber = '"+txtTicketNumber.Text.ToString()+"'");
//check DataReader
if (DataReader == null)
{
DataReader.Close();
DataReader.Dispose();
}
else
{
if (DataReader.HasRows)
{
ReaderResult.Clear();
while (DataReader.Read())
{
for (int i = 0; i < DataReader.FieldCount; i++)
{
if (DataReader.IsDBNull(i))
{
string CaseNull = "";
ReaderResult.Add(CaseNull);
}
else
{
//put results in LIST<>
ReaderResult.Add(DataReader.GetString(i));
}
}
}
//I HAVE TO ASSIGN THE VALUES RETURNED TO textBox1 - textBox5 HERE
}
else
{
DataReader.Close();
DataReader.Dispose();
}
DataReader.Close();
DataReader.Dispose();
}
I have 5 text boxes. I cant figure out how to assign the tax codes to the textboxes.
我有5个文本框。我无法弄清楚如何将税码分配给文本框。
I tried the IF-ELSE (taking as reference the number of rows in datareader) but by this method i have no way to delegate the outputs to different text boxes.
我尝试了IF-ELSE(以datareader中的行数作为参考)但是通过这种方法我无法将输出委托给不同的文本框。
Any idea of how to achieve this goal?
知道如何实现这一目标吗?
3 个解决方案
#1
1
Put all textboxes in an array. Make sure that they are in the same order as your fields are.
将所有文本框放在一个数组中。确保它们与您的字段的顺序相同。
var textBoxes = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5};
for (int i = 0; i < DataReader.FieldCount; i++)
{
if (DataReader.IsDBNull(i))
{
string CaseNull = "";
ReaderResult.Add(CaseNull);
}
else
{
//put results in LIST<>
ReaderResult.Add(DataReader.GetString(i));
}
// put data i onto textbox i
textBoxes[i].Text = DataReader.GetString(i);
}
#2
0
//I HAVE TO ASSIGN THE VALUES RETURNED TO textBox1 - textBox5 HERE
textBox1.Text = ReaderResult.ElementAtOrDefault(0);
textBox2.Text = ReaderResult.ElementAtOrDefault(1);
textBox3.Text = ReaderResult.ElementAtOrDefault(2);
textBox4.Text = ReaderResult.ElementAtOrDefault(3);
textBox5.Text = ReaderResult.ElementAtOrDefault(4);
However, that depends on if your reader contains only one record with 5 fields or if it contains multiple records with n-fields. In that case you need to clarify your question.
但是,这取决于您的阅读器是否只包含一个包含5个字段的记录,或者它是否包含多个具有n字段的记录。在这种情况下,您需要澄清您的问题。
Side-note: you should always use sql-parameters to prevent sql-injection or localization issues, so instead of
旁注:你应该总是使用sql-parameters来防止sql注入或本地化问题,所以不要使用
... where h.DocumentNumber = '"+txtTicketNumber.Text.ToString()+"'"
this
这个
... where h.DocumentNumber = @TicketNumber"
and you should modify your DoQuery
method to allow to pass parameters.
你应该修改你的DoQuery方法以允许传递参数。
#3
0
Create a list of textboxes, and assign the result in a loop :
创建文本框列表,并在循环中分配结果:
var tbs = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5 };
for(int index = 0; index < ReaderResult.Count, index++)
{
if(index < tbs.Count)
{
tbs[index].Text = ReaderResult[index];
}
}
An advice, your code can be simplify using the IDisposable
and using
pattern :
建议,您的代码可以使用IDisposable和使用模式进行简化:
using (var DataReader = DoQuery(""))
{
if (DataReader.HasRows)
{
ReaderResult.Clear();
while (DataReader.Read())
{
for (int i = 0; i < DataReader.FieldCount; i++)
{
if (DataReader.IsDBNull(i))
{
string CaseNull = "";
ReaderResult.Add(CaseNull);
}
else
{
//put results in LIST<>
ReaderResult.Add(DataReader.GetString(i));
}
}
}
}
}
#1
1
Put all textboxes in an array. Make sure that they are in the same order as your fields are.
将所有文本框放在一个数组中。确保它们与您的字段的顺序相同。
var textBoxes = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5};
for (int i = 0; i < DataReader.FieldCount; i++)
{
if (DataReader.IsDBNull(i))
{
string CaseNull = "";
ReaderResult.Add(CaseNull);
}
else
{
//put results in LIST<>
ReaderResult.Add(DataReader.GetString(i));
}
// put data i onto textbox i
textBoxes[i].Text = DataReader.GetString(i);
}
#2
0
//I HAVE TO ASSIGN THE VALUES RETURNED TO textBox1 - textBox5 HERE
textBox1.Text = ReaderResult.ElementAtOrDefault(0);
textBox2.Text = ReaderResult.ElementAtOrDefault(1);
textBox3.Text = ReaderResult.ElementAtOrDefault(2);
textBox4.Text = ReaderResult.ElementAtOrDefault(3);
textBox5.Text = ReaderResult.ElementAtOrDefault(4);
However, that depends on if your reader contains only one record with 5 fields or if it contains multiple records with n-fields. In that case you need to clarify your question.
但是,这取决于您的阅读器是否只包含一个包含5个字段的记录,或者它是否包含多个具有n字段的记录。在这种情况下,您需要澄清您的问题。
Side-note: you should always use sql-parameters to prevent sql-injection or localization issues, so instead of
旁注:你应该总是使用sql-parameters来防止sql注入或本地化问题,所以不要使用
... where h.DocumentNumber = '"+txtTicketNumber.Text.ToString()+"'"
this
这个
... where h.DocumentNumber = @TicketNumber"
and you should modify your DoQuery
method to allow to pass parameters.
你应该修改你的DoQuery方法以允许传递参数。
#3
0
Create a list of textboxes, and assign the result in a loop :
创建文本框列表,并在循环中分配结果:
var tbs = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5 };
for(int index = 0; index < ReaderResult.Count, index++)
{
if(index < tbs.Count)
{
tbs[index].Text = ReaderResult[index];
}
}
An advice, your code can be simplify using the IDisposable
and using
pattern :
建议,您的代码可以使用IDisposable和使用模式进行简化:
using (var DataReader = DoQuery(""))
{
if (DataReader.HasRows)
{
ReaderResult.Clear();
while (DataReader.Read())
{
for (int i = 0; i < DataReader.FieldCount; i++)
{
if (DataReader.IsDBNull(i))
{
string CaseNull = "";
ReaderResult.Add(CaseNull);
}
else
{
//put results in LIST<>
ReaderResult.Add(DataReader.GetString(i));
}
}
}
}
}