0

我想在用户注册的同一网页中显示注册数据库。我可以通过使用列表视图配置数据库来显示。

但我想通过在 c# 中使用 add item 命令来做到这一点。

另一个问题是即使在我添加了listview (system.web.ui.webcontrols)Visualstudio 的引用后,当我键入 listview 时它仍然模棱两可。

我的代码在下面

        public void Insertfunc()//inserting into database 
        {
            string database = @"Data Source=.;Initial Catalog=NewDB;Integrated Security=True";
            SqlConnection myConn = new SqlConnection(database);
            string queryStr = @"insert into Registration values (@fname,@lname,@dob,@emailid,@uname)";
            SqlCommand myCommand = new SqlCommand(queryStr, myConn);
            myCommand.Parameters.AddWithValue("@fname", FirstName);
            myCommand.Parameters.AddWithValue("@lname", LastName);
            myCommand.Parameters.AddWithValue("@dob", DateofBirth);
            myCommand.Parameters.AddWithValue("@emailid", Mailid);
            myCommand.Parameters.AddWithValue("@uname", UserName);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myConn.Close();

            string query = @"select * from Registration";
            SqlCommand mycommand1 = new SqlCommand(query,myConn);
            SqlDataReader tbl = mycommand1.ExecuteReader();
// i just want to add the listview coding here 

        }

    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Submit_Click(object sender, EventArgs e)
    {
        UserRegistration User1 = new UserRegistration();
        int flag = 0;

       User1.Mailid = mailid.Text;
       User1.UserName = UN.Text;
       User1.FirstName = FN.Text;
       User1.LastName = LN.Text;
       User1.DateofBirth = DOB.Text;
     if (User1.Validatefunc(User1.Mailid, "[Email]") == true)
        {
            IDlbl.Text = "Provide Someother mailid";
        }
        else
        {
            IDlbl.Text = "Okay";
            flag = 1;

        }

        if (User1.Validatefunc(User1.UserName, "[Username]") == true)
        {
            UNlbl.Text = "Username Not Available";

        }
        else
        {
            flag++;
            UNlbl.Text = "Username Available";
        }
        if (flag == 2)
        {
            User1.Insertfunc();
        }


    }


}
4

2 回答 2

1

使用代码使用列表视图显示数据

protected void Page_Load(object sender, EventArgs e)
        {
            ListView1.DataSource = this.GetData();
            ListView1.DataBind();
        }

        private DataSet GetData()
        {
            string conString = ConfigurationManager.ConnectionStrings["Connectionstr"].ConnectionString;
            string query = "SELECT * FROM Registration";
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);
                        return ds;
                    }
                }
            }
        }
于 2013-11-11T07:07:10.153 回答
0

ASPX 代码

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1">
    <ItemTemplate>

        <strong>Country Id : </strong>
        <asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>'></asp:Label>
        <br />
        <strong>Country Name :</strong>
        <asp:Label runat="server" ID="lblName" Text='<%# Eval("CountryName") %>'></asp:Label>

        </div>
    </ItemTemplate>
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
    </LayoutTemplate>
</asp:ListView>

aspx.cs 代码

SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest2;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from tblCountry", con);
sda.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind(); 

希望它可以帮助你欢呼

于 2015-10-06T21:44:48.440 回答