E Benzle
E Benzle

Reputation: 346

How to pull data from a SQL Query based on data/id relationship. Using ASP.net WebMatrix

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

Upvotes: 1

Views: 1184

Answers (1)

ristonj
ristonj

Reputation: 1608

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" />
}

Upvotes: 2

Related Questions