1

I'm using ASP.net WebMatrix

I have a sql query which looks like this:

var queryformdata = db.Query("SELECT * FROM document_data WHERE doc_id = @0", 1534);

doc_id      field_data      field_name
----------  ----------      ----------
1534        John            f_name
1534        61st Street     f_address

And input fields which look like this:

<input type="text" id="f_name" name="f_name" value="" />
<input type="text" id="f_address" name="f_address" value="" />

I want the value of "John" to appear for f_name and "61st Street" to appear for f_address

I know the value of field_data, but don't know how to pull that from the query without doing a separate query for every input field. I found a C# function called .Select() but can't get it to work. Here is what I tried:

@{
    DataRow[] foundRows;
    foundRows = queryformdata.Select("field_name LIKE 'f_name'");                
}
<input type="text" id="f_name" name="f_name" value="@foundRows.field_data" />

That gives me the error: Compiler Error Message: CS0411: The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Can someone help me figure out how to make this work.

Thanks

E Benzle
  • 346
  • 3
  • 15

1 Answers1

2

Instead of:

@{
DataRow[] foundRows;
foundRows = queryformdata.Select("field_name LIKE 'f_name'");                

}

Try this:

@foreach(var queryformdata = db.Query("SELECT * FROM document_data WHERE doc_id = @0", 1534)) {
    <input type="text" id="@queryformdata.field_name" name="@queryformdata.field_name" value="@queryformdata.field_data" />
}
ristonj
  • 1,590
  • 1
  • 12
  • 15
  • I was actually trying to avoid doing a database call for each input field. There are over 100 fields on some of the forms and it seems like there should be a way to assign the data without making a separate call for each. Maybe I'm wrong though. Is the best way to just make multiple db requests? – E Benzle Jun 19 '13 at 18:50
  • This should only make one request to the DB. It will loop through the results of the request and assign the data. – ristonj Jun 20 '13 at 00:28