By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

problem with datatable

P: n/a
ven
Hello i have a dynamic datatable in my page. I wanna to write data to
textboxes and after click on button "ADD data" i want to refresh datagrid on
page... Here is my code :

' Insert page code here
'

Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable
Dim row as system.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
if Not IsPostBack Then
make_d_tables()
End if
End Sub

Function make_d_tables()

wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))

zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))

End Function

Sub Button2_Click(sender As Object, e As EventArgs)

row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(rowe)

datagrid1.datasource=wykszt
datagrid1.databind

End Sub

.....
but something not works....
i have an exception with this code but when i use page_init like :

Private Sub Page_init(s As Object, e As EventArgs)
make_d_tables()
End Sub

it`s start to works but in datagrid i have all time one position. I think
that page_init is called all the time i click button "add data" and
initialise tables all the time
I wanna add rows to datatable after clickin "Add data" and to dynamicaly
refresh datagrid ??? how to make it ????
And one more question
After click on button page is reloaded and set on the top of the page. How
can i do thaT after click page is in the same place like before ???

Thanks

Pat
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ven
Sorry this line isnt wykszt.Rows.Add(rowe) but wykszt.Rows.Add(row)

Użytkownik "ven" <ve*****@poczta.onet.pl> napisał w wiadomo¶ci
news:ci*********@news.onet.pl...
Hello i have a dynamic datatable in my page. I wanna to write data to
textboxes and after click on button "ADD data" i want to refresh datagrid on page... Here is my code :

' Insert page code here
'

Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable
Dim row as system.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
if Not IsPostBack Then
make_d_tables()
End if
End Sub

Function make_d_tables()

wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))

zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))

End Function

Sub Button2_Click(sender As Object, e As EventArgs)

row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(rowe)

datagrid1.datasource=wykszt
datagrid1.databind

End Sub

....
but something not works....
i have an exception with this code but when i use page_init like :

Private Sub Page_init(s As Object, e As EventArgs)
make_d_tables()
End Sub

it`s start to works but in datagrid i have all time one position. I think
that page_init is called all the time i click button "add data" and
initialise tables all the time
I wanna add rows to datatable after clickin "Add data" and to dynamicaly
refresh datagrid ??? how to make it ????
And one more question
After click on button page is reloaded and set on the top of the page. How
can i do thaT after click page is in the same place like before ???

Thanks

Pat

Nov 18 '05 #2

P: n/a
> but something not works....
i have an exception with this code but when i use page_init like :
You will have an exception with this code. When the page posts back, you
are not creating the DataTables.
wykszt will be null. I am guessing you are receiving and NullException or a
object not set exception.

If you put your Page_Init, into the Page_Load but remove the IsPostBack
check, you will no longer receive the exception.

I would recommend (others will probably lambast me over this) not placing
anything into the Page_Init method until you are more comfortable with the
framework. I have spent many hours debugging control loading and certain
controls not being completely loaded.

With your code as so, you will be adding one line to a DataGrid, and the
next time they hit the "Add" button, it will essentially replace the
previous line with the new line. To keep adding rows to the DataGrid, you
should use either Session or ViewState. Viewstate if you don't have many
rows, Session if you don't have many users.

As far as saving the scroll position on postback. You can turn on Smart
Navigation or write your own. I have read countless problems with Smart
Navigation so I would suggest writing your own. You will need to use client
side script and add onto the "onsubmit" handler for the form.

This is by no means good code, but you will get the gist. Infact, this is
no where near production grade code. Hell, I don't even know if this is
correct syntax. I am c# guy.

sub Page_Load(..)

'check to see if it is in session memory.
if Session("wykszt") IS Nothing then
make_d_table()
end if

if Not IsPostBack Then
datagrid1.datasource= Session( "wykszt" )
datagrid1.databind
end if
end sub

Sub Button2_Click(sender As Object, e As EventArgs)

Dim wykszt as System.Data.DataTable

'pull it out of session.
wykszt = Session( "wykszt" )

row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(rowe)

datagrid1.datasource=wykszt
datagrid1.databind
End Sub
Function make_d_tables()
Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable

wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))

zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))

'this will save it into session memory.
Session("wykszt") = wykszt
Session( "zatru" ) = zatru
End Function

HTH,

bill

"ven" <ve*****@poczta.onet.pl> wrote in message
news:ci*********@news.onet.pl... Hello i have a dynamic datatable in my page. I wanna to write data to
textboxes and after click on button "ADD data" i want to refresh datagrid on page... Here is my code :

' Insert page code here
'

Dim wykszt as System.Data.DataTable
Dim zatru as System.Data.DataTable
Dim row as system.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
if Not IsPostBack Then
make_d_tables()
End if
End Sub

Function make_d_tables()

wykszt = New System.Data.DataTable("wykszt")
wykszt.Columns.Add("od_data", GetType(date))
wykszt.Columns.Add("do_data", GetType(date))
wykszt.Columns.Add("nazwa_szkoly", GetType(String))
wykszt.Columns.Add("nazwa_tytulu", GetType(String))

zatru = New System.Data.DataTable("zatr")
zatru.Columns.Add("od_data", GetType(date))
zatru.Columns.Add("do_data", GetType(date))
zatru.Columns.Add("nazwa_prac", GetType(String))
zatru.Columns.Add("nazwa_stan", GetType(String))

End Function

Sub Button2_Click(sender As Object, e As EventArgs)

row = wykszt.NewRow
row("od_data")=TextBox27.Text + "-01"
row("do_data")=TextBox28.Text + "-01"
row("nazwa_szkoly")=TextBox29.Text
row("nazwa_tytulu")=TextBox30.Text
wykszt.Rows.Add(rowe)

datagrid1.datasource=wykszt
datagrid1.databind

End Sub

....
but something not works....
i have an exception with this code but when i use page_init like :

Private Sub Page_init(s As Object, e As EventArgs)
make_d_tables()
End Sub

it`s start to works but in datagrid i have all time one position. I think
that page_init is called all the time i click button "add data" and
initialise tables all the time
I wanna add rows to datatable after clickin "Add data" and to dynamicaly
refresh datagrid ??? how to make it ????
And one more question
After click on button page is reloaded and set on the top of the page. How
can i do thaT after click page is in the same place like before ???

Thanks

Pat

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.