I'm stuck with code that is failing to bind. It's not using SQL, it's just a simple form using Response.Write in the codebehind.
Here's the code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Populate DataTable
Dim dt As New DataTable()
dt.Columns.Add("fname")
dt.Columns.Add("lname")
dt.Rows.Add()
dt.Rows(0)("fname") = Response.ContentType = "fname"
dt.Rows(0)("lname") = Response.ContentType = "lname"
'Bind Datatable to Labels
lblfname.Text = dt.Rows(0)("fname").ToString()
lbllname.Text = dt.Rows(0)("lname").ToString()
End If
End Sub
Related
let say i have 2 text box, 1 button and 1 gridview and i want to add data from textboxs to gridview with a button click.
How can I achive it.
I have tried below code but it replaced the old row.
Private dt As New DataTable
Private Sub Btnidadd_Click(sender As Object, e As EventArgs) Handles Btnidadd.Click
dt.Columns.Add("First Name")
dt.Columns.Add("Last Name")
Dim R As DataRow = dt.NewRow
R("First Name") = textbox1.Text
R("Last Name") = textbox2.Text
dt.Rows.Add(R)
GridView1.DataSource = dt
GridView.DataBind()
When you click the button in ASP.NET, a postback occurs and the page is recreated.
So, you need to keep data somewhere.
For example, if you save it to a session variable like this.
Private dt As DataTable
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
dt = New DataTable
dt.Columns.Add("First Name")
dt.Columns.Add("Last Name")
Session("dt") = dt
Else
dt = CType(Session("dt"), DataTable)
End If
End Sub
Private Sub Btnidadd_Click(sender As Object, e As EventArgs) Handles Btnidadd.Click
Dim R As DataRow = dt.NewRow
R("First Name") = TextBox1.Text
R("Last Name") = TextBox2.Text
dt.Rows.Add(R)
GridView1.DataSource = dt
GridView1.DataBind()
Session("dt") = dt
End Sub
I am wondering how to add rows to an already populated datatable with a button. the code I have so far is this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable("Dt")
dt.Columns.Add("test1")
dt.Columns.Add("test2")
dt.Columns.Add("test3")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
Dim dt As New DataTable
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
it works for me. Because am I storing the DataTable in ViewState so that I can reference the current data associated within the DataTable when it postback.
You are initializing DataTable in Page_Load method including header of DataTable. When you are clicking on btnadd button then you are not getting existing DataSource. If you assign dt in ViewState("DataSource ") in Page_Load method and declare again DataTable in btnAdd_Click method like Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable) then it will work because previous DataSource you are getting now. Finally I want to say that DataSource doesn't remember the DataSource you set in a previous rendering of the page.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As New DataTable("Dt")
dt.Columns.Add("test1")
dt.Columns.Add("test2")
dt.Columns.Add("test3")
GridView1.DataSource = dt
GridView1.DataBind()
ViewState("DataSource ") = dt
End Sub
Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
dt.Rows.Add("Hi1", "Hi2", "Hi2")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
I got a Dropdownlist filled from a Database, using the ID from the Database as ValueField but it just doesn't work
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim dtb As DataTable
Dim strSql As String = "SELECT * FROM Personen"
dtb = New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.Items.Clear()
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End Sub
When I try using ddlName.SelectedItem.Value later i get 1 for every Item.
The Using Code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dateString As String = tbDate.Text
Dim name As String = ddlName.SelectedItem.Text
Dim des As String = tbDescription.Text
MsgBox(ddlName.SelectedItem.Value)
You don't have to DataBind the DropDownList on every postback if viewstate is enabled(default). That will overwrite all changes like the SelectedIndex. Instead put the code in Page_Load in a Not Is PostBack check:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim strSql As String = "SELECT * FROM Personen"
Dim dtb As New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End If
End Sub
Side-Note: you should also not reuse the connection. Instead create, initialize and close it in the method where you use it, best by using the Using-statement which also ensures that it get's disposed/closed in case of an error.
I want to color single Rows when there empty in a Gridview, but it changes the color from every Row empty and not.
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim Menge As Integer = GridView1.Rows.Count - 1
For i = 0 To Menge
For Each row As GridViewRow In GridView1.Rows
If GridView1.Rows(i).Cells(4).Text = "" Then
GridView1.Rows(i).Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
Next
End Sub
Can someone Help me?
You should use the following code in your Default_Load event, but this will only color rows when page loads. Ideally, you should hook up the RowDataBound event with your grid view.
Code if using Default_Load event
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
'color rows based on some condition
For Each row As GridViewRow In GridView1.Rows
If row.Cells(4).Text = "" Then
row.Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
End Sub
Code if using RowDataBound event
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(4).Text = "" Then
e.Row.Cells(4).BackColor = System.Drawing.Color.Red
End If
End If
End Sub
I am able to export a gridview to excel, my problem is that I cannot figure out how to remove the formatting from coming over from the girdview. Here is the code I am using to export the gridview:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Clear()
Response.Charset = ""
'Response.ContentType = "application/vnd.ms-excel"
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Dim stringWrite = New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
GridView1.GridLines = GridLines.None
GridView1.HeaderStyle.Font.Bold = True
GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataBind()
GridView1.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()
End Sub
What I suggest that you should iterate datasource , append the content of each row into StringBuilder object and write that string to the response buffer.