Saturday, March 31, 2012
There is no row at ...
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Panel2.Visible = false;
Panel1.Visible = true;
Label1.Text = "Logged in as: " + Session["VisitorsName"];
sqlConnection1.Open();
sqlFilesIDAdapter.SelectCommand.Parameters["@dotnet.itags.org.ID"].Value =
Session["VisitorsID"];
sqlFilesIDAdapter.Fill(filesIDData1,"Files");
DataGrid1.DataBind();
}
}
private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = true;
Label2.Text =
filesIDData1.Tables["Files"].Rows[DataGrid1.SelectedItem.ItemIndex]["TP_
Name"].ToString();
}
Now when I click on a row I get: 'There is no row at ' and then the
number. If I remove the !IsPostBack condition it works. Why is this?
--
Kind regards,
Stijn Verrept.Because the data is loaded the first time into the grid, but the second time
you haven't loaded the data therefore the grid is empty when you change the
index. You could store the object in the Viewstate and then recover it on
further loads, though this can make for a rather large page as the Viewstate
data tends to bloat a page pretty well.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"Stijn Verrept" <stijn@.no_s.p.a.n.entrysoft.com> wrote in message
news:400dc686$0$323$ba620e4c@.news.skynet.be...
> I have this code so far:
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (!IsPostBack)
> {
> Panel2.Visible = false;
> Panel1.Visible = true;
> Label1.Text = "Logged in as: " + Session["VisitorsName"];
> sqlConnection1.Open();
> sqlFilesIDAdapter.SelectCommand.Parameters["@.ID"].Value =
> Session["VisitorsID"];
> sqlFilesIDAdapter.Fill(filesIDData1,"Files");
> DataGrid1.DataBind();
> }
> }
> private void DataGrid1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> Panel1.Visible = false;
> Panel2.Visible = true;
> Label2.Text =
> filesIDData1.Tables["Files"].Rows[DataGrid1.SelectedItem.ItemIndex]["TP_
> Name"].ToString();
> }
>
> Now when I click on a row I get: 'There is no row at ' and then the
> number. If I remove the !IsPostBack condition it works. Why is this?
> --
> Kind regards,
> Stijn Verrept.
Mark Fitzpatrick wrote:
> Because the data is loaded the first time into the grid, but the
> second time you haven't loaded the data therefore the grid is empty
> when you change the index. You could store the object in the
> Viewstate and then recover it on further loads, though this can make
> for a rather large page as the Viewstate data tends to bloat a page
> pretty well.
Thanks Mark for your answer, I understand now. But what I don't get is
why I need to reload the data if all I do is show a field in a label?
Is it possible to do this client side?
--
Kind regards,
Stijn Verrept.
I agree, I don't know that I would recommend the viewstate because it can get pretty large, depending on how much data you're talking about
One of the things I've done is if the page is not a postback then load the grid. On the SelectedIndexChanged event on the grid I do all the manipulation and whatever I need to do, then I do a response.redirect to that same page I'm already on. This works pretty well (I think) if you're programmatically doing something with the data and leaving the user out of any data manipulation. However, if you need the user to manipulate the data this won't work so well (I don't think)
HTH
Jeremy
Stijn
The reason you need to reload the data is because when a postback happens you loose all of data. I know it seems bizarre, but that's how the web works. Every HTTP request to a file (or page) acts like a brand new request, that's why in your pages you'll need to put in the appropriate checks to ensure the page is a PostBack or not
I'm not sure about working with the grid on the client though. But I hope this answers your question about why you need to reload the data
Jeremy
Stijn Verrept wrote:
> Mark Fitzpatrick wrote:
>
>>Because the data is loaded the first time into the grid, but the
>>second time you haven't loaded the data therefore the grid is empty
>>when you change the index. You could store the object in the
>>Viewstate and then recover it on further loads, though this can make
>>for a rather large page as the Viewstate data tends to bloat a page
>>pretty well.
>
> Thanks Mark for your answer, I understand now. But what I don't get is
> why I need to reload the data if all I do is show a field in a label?
> Is it possible to do this client side?
If you are looking up a value in the same table you bound to, just
create some invisible/hidden columns with the other column values you
need later on.
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Craig Deelsnyder wrote:
> If you are looking up a value in the same table you bound to, just
> create some invisible/hidden columns with the other column values you
> need later on.
Aha! Thanks :)
I changed it to Label2.Text = DataGrid1.SelectedItem.Cells[1].Text; and
this works great. I also replaced the Dataset by a DataReader which
seems to speed it up some more :)
--
Kind regards,
Stijn Verrept.
Wednesday, March 28, 2012
This code doesnt work with VS2005
Dim MyConn As New SqlConnection("Server=localhost;UID=sa;PWD=;database=Northwind")
MyConn.Open()
Dim StrSelect As String = "Select * FROM Customers"
Dim SqlKu As SqlCommand = New SqlCommand(StrSelect, MyConn)
Dim MyReader As SqlDataReader
MyReader = SqlKu.ExecuteReader()
DataList1.DataSource = MyReader
DataList1.DataBind()
MyReader.Close()
MyConn.Close()
End Sub
There's nothing error occur.. just blank, please help me to fix it.
Is it with VS2005 we can write codes manually just like we do with VS2003?
Thanks.
.NET Framework 2.0 has a lot of new controls that cut down on the amount of code you have to write. Two things I recommend in the code you provided are to open the connection as late as possible and to use try / finally or try / catch / finally in your code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyConn As SqlConnection = Nothing
Dim MyReader As SqlDataReader = Nothing
Try
MyConn = New SqlConnection("Server=localhost;UID=sa;PWD=;database=Northwind")
Dim StrSelect As String = "Select * FROM Customers"
Dim SqlKu As New SqlCommand(StrSelect, MyConn)
MyConn.Open()
MyReader = SqlKu.ExecuteReader()
DataList1.DataSource = MyReader
DataList1.DataBind()
Finally
If Not MyReader Is Nothing Then
If Not MyReader.IsClosed Then
MyReader.Close()
End If
End If
If Not MyConn Is Nothing Then
If MyConn.State = ConnectionState.Open Then
MyConn.Close()
End If
End If
End Try
End Sub
If the above code doesn't work, then there is some issue with your connection to the database or the query itself.
HTH,
Ryan
Hi,
another interesting read is this:Open() late, Close() early
Grz, Kris.
Thursday, March 22, 2012
thread being aborted?
Dim connstring As New SqlConnection("Server=kabunyawan\sqlexpress;Database=ojt;Trusted_Connection=True;")
Dim rs1 As New SqlDataAdapter("select * from wall where uname = '" & txtuname.Text & "' and pword = '" & txtpword.Text & "'", connstring)
Dim ds As New DataSet
Dim tmpStr As String
rs1.Fill(ds, "wall")
Try
tmpStr = ds.Tables("wall").Rows(0).Item("lvl")
If tmpStr = "admin" Then
Response.Redirect("contact.aspx")
Else
Response.Write(tmpStr)
Response.Write("admin")
Response.Write("1111111111111111")
End If
Catch ex As Exception
Response.Cookies("errmsg").Value = "Invalid username/password. Please contact the admins for assisstance."
Response.Redirect("merror.aspx")
End Try
End Sub
i am having error on response.redirect("contact.aspx").. but my other webforms are working..Problem Solved.. that error was caused by the "try-catch" part.. I've read that response.redirect will always give out an error if placed within a try-catch.. now i know!
Now read that Response.Write is a bad thing on ASP.NET pages. :)