I am using a Repeater
in Visual Studio (asp.net and c#). The thing is, I am not quite sure how to properly format my stored procedure.
Here is the error:
My goal is to have a Repeater
display a label and textbox for each column in my table. The label must have the DisplayName
from the database.
Here is my table, FormField
, that I want to use for my Repeater
.
So here is my idea for my stored procedure at the moment:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spGetFormFields]
@TableName nvarchar,
@EventId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COLUMN_NAME AS MyColumn
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND EventId = @EventId
AND Visible = 1
END
However this doesn't quite work, as I get errors at EventId
and Visible
:
Invalid Column Name
So here is what I have in my C#:
protected void Page_Load(object sender, EventArgs e)
{
//Make label names diff
//Control which columns are shown
//Style the labels and textboxes to css
DataTable table = new DataTable();
table.Columns.Add("MyColumn", typeof(string));
using (SqlConnection sqlConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
{
sqlConn2.Open();
using (SqlCommand sqlCmd2 = new SqlCommand())
{
sqlCmd2.Connection = sqlConn2;
sqlCmd2.CommandType = System.Data.CommandType.Text;
sqlCmd2.CommandText = string.Format("SELECT COLUMN_NAME AS MyColumn FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND EventId = @EventId AND Visible = 1");
sqlCmd2.Parameters.Add("@TableName", SqlDbType.NVarChar).Value = "FormField";
sqlCmd2.Parameters.Add("@EventId", SqlDbType.NVarChar).Value = "1";
sqlCmd2.ExecuteNonQuery();
using (SqlDataReader sqlReader = sqlCmd2.ExecuteReader())
{
while (sqlReader.Read())
{
DataRow tableRow = table.NewRow();
tableRow["MyColumn"] = sqlReader["MyColumn"].ToString();
table.Rows.Add(tableRow);
}
Repeater1.DataSource = table;
Repeater1.DataBind();
}
sqlConn2.Close();
}
}
}
}
My C# code implements the SQL without any stored procedure (still gets the same error about the columns not existing). Of course I want to add in the stored procedure once I get it to work, so this was for testing purposes.
And finally, here is the asp.net markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeater.aspx.cs" Inherits="admin_repeater" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label>
<asp:TextBox ID="textTest" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
As mentioned in the comment, You are querying INFORMATION_SCHEMA.COLUMNS
table and there is no such column EventId
& Visible
there. If I have understood you correctly you want to display DisplayName
from your table based on condition then you can directly do it like this:-
SELECT DisplayName AS MyColumn
FROM [Events2].dbo.[FormField]
WHERE EventId = @EventId AND
Visible = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.