Ram
Ram

Reputation: 49

Display data using ListView in ASP.NET

I want to display the registration database in the same webpage where user registers. I can display by using the listview by configuring the database with it.

But i want to do that by using add item command in c#.

Another problem is even after i added the reference for the listview (system.web.ui.webcontrols) the Visualstudio says its ambiguous when i type listview.

my code is below

        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();
        }


    }


}

Upvotes: 0

Views: 13787

Answers (2)

ABi
ABi

Reputation: 143

ASPX Code

<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 code

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(); 

Hope it helps u cheers

Upvotes: 0

Ram
Ram

Reputation: 49

To display the data using listview by using codes

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;
                    }
                }
            }
        }

Upvotes: 1

Related Questions