Click here to Skip to main content
16,012,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i want to display the datalist control after the button click event occurs.
i have this code:
VB
Imports system.data
Imports System.Data.SqlClient

Partial Class aboutus
    Inherits System.Web.UI.Page
    Dim c As String = "Data Source=VALUED-P0B2H9XS\SQLEXPRESS;Initial Catalog=dictionary1;Integrated Security=True"

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(c)
        Dim s As String = "INSERT INTO postcomment (pname,pemail,pcomment,pdate) VALUES('" + TextBox1.Text + "', '" +    TextBox2.Text + "', '" + TextBox3.Text + "','" + DateTime.Now + "');"
        Dim com As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(s, con)
        com.Connection = con
        con.Open()
        Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        adapter.SelectCommand = com
        Dim dictionary1 As System.Data.DataSet = New System.Data.DataSet()
        adapter.Fill(dictionary1, "postcomment")
        con.Close()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(c)
        Dim s1 As String = "SELECT * FROM postcomment;"
        Dim com1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(s1, con1)
        com1.Connection = con1
        con1.Open()
        Dim adapter2 As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        adapter2.SelectCommand = com1
        Dim dictionary1 As System.Data.DataSet = New System.Data.DataSet()
        adapter2.Fill(dictionary1, "postcomment")
        Dim reader As SqlDataReader
        reader = com1.ExecuteReader()
        reader.Read()
        If reader.Read() Then
            DataList1.DataSource = dictionary1.Tables("postcomment")
            Me.DataBind()
        Else
            DataList1.Visible = False
            nocomment.Text = "No Comments Posted"
        End If
        reader.Close()
        con1.Close()
    End Sub
End Class

it does not work properly. i think there is lot of mistakes. someone help me.
Posted
Updated 1-Oct-11 21:03pm
v2

1 solution

First of All you need to create a Item Templet of DataList in which data will be displayed, then try this...

VB
If reader.Read() Then
            DataList1.DataSource = dictionary1.Tables("postcomment")
            Me.DataBind()
        Else
            DataList1.Visible = False
            nocomment.Text = "No Comments Posted"
        End If


Inseted of Me.DataBind() please try to call a bind method of

DataList1.DataBind()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900