顯示具有 AccessDataSource 標籤的文章。 顯示所有文章
顯示具有 AccessDataSource 標籤的文章。 顯示所有文章

2014年7月13日 星期日

[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] Gridview update


in my asp.net+vb web with access database. i used the following code. data is displaying correctly but when edit/update is done data is not saved


                    DataSourceID="SqlDataSource1" AutoGenerateEditButton="True">







ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>"

SelectCommand="SELECT [STAFF], [NAME], [COURSE] FROM [STUFF]"
UpdateCommand="UPDATE [STUFF] SET [NAME] = @NAME, [COURSE] = @COURSE WHERE [STAFF] = @STAFF">








I think you need to call 


SqlDataSource1.Update() method  , then it will update records 




Where is your update parameters?


When using updatecommand with parameters, you need to set update parameters







Usualy, updating data with accessdatasource/sqldatasource in gridview we need pimary key to determine which row you want to update


The primary key value passed through datakeynames



And updatecommand and updateparameters looks like this


UpdateCommand="UPDATE [STUFF] SET [NAME] = ?, [COURSE] =?,  [STAFF] = ? WHERE ID=?">
















[RESOLVED] getting problem while updation


hey guys..


i have created a gridview using ms access database


source below.



        DataKeyNames="bill_no" DataSourceID="AccessDataSource1">


SortExpression="bill_no" />
SortExpression="bill_date" />

SortExpression="district" />
SortExpression="branch" />
SortExpression="total_amt" />
SortExpression="comission" />
SortExpression="account_type" />
SortExpression="bill_date_range" />
SortExpression="remarks" />


DataFile="~/App_Data/daksh.mdb"
DeleteCommand="DELETE FROM [bills] WHERE (([bill no] = ?) OR ([bill no] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [bills] ([bill no], [bill date], [bank], [district], [branch], [total amt], [comission], [account type], [bill date range], [remarks]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
SelectCommand="SELECT [bill no] AS bill_no, [bill date] AS bill_date, [bank], [district], [branch], [total amt] AS total_amt, [comission], [account type] AS account_type, [bill date range] AS bill_date_range, [remarks] FROM [bills]"
UpdateCommand="UPDATE [bills] SET [bill date] = ?, [bank] = ?, [district] = ?, [branch] = ?, [total amt] = ?, [comission] = ?, [account type] = ?, [bill date range] = ?, [remarks] = ? WHERE (([bill no] = ?) OR ([bill no] IS NULL AND ? IS NULL))">




























whenever i am using update option its giving me this error.


Exception Details:
System.Data.OleDb.OleDbException: No value given for one or more required parameters.




Remarks: i have the same colums in both database and gridview



Hi,


You may miss some fields in you INSERT Query. Its my guess. Please ensure that.


Hope this helps!


Thanks!



You have 10 update parameters only, so try this : UpdateCommand = "UPDATE [bills] SET [bill date] = ?, [bank] = ?, [district] = ?, [branch] = ?, [total amt] = ?, [comission] = ?, [account type] = ?, [bill date range] = ?, [remarks] = ? WHERE [bill no] = ?"
change delete command also because i think deleting will also not working.

nope. i have 10 columns in the database and here also it have 10 fields to be updated...




You have 11 "?"s in update command and 2 in delete command, you need one more parameter. But you only have bill no

[RESOLVED] DetailsView to stop updating a certain field


So i have a database for the customers details and i have set up a DetailsView so that the customers can view their details and edit them and update them. It updates but the problem is that it gets the users details via the UserName, I set the UserName property
to not be able to edit it as i want to leave it as default, but when i update it updates everything except the UserName, so all i can do is just leave it to show to UserName property but i dont want that because i dont want the user to edit his username. Heres
the code to it:



DataFile="~/App_Data/ASPNetDB.mdb"

InsertCommand="INSERT INTO [tbl_Customer] ([UserID], [Title], [UserName], [Firstname], [Surname], [Address], [Postcode], [Mobile_Number], [Email], [Rental_Number]) VALUES (@UserID, @Title, @UserName, @Firstname, @Surname, @Address, @Postcode, @Mobile_Number,
@Email, @Rental_Number)"

SelectCommand="SELECT * FROM [tbl_Customer] WHERE ([UserName] = )"

UpdateCommand="UPDATE [tbl_Customer] SET [Title] = @Title, [UserName] = @UserName, [Firstname] = @Firstname, [Surname] = @Surname, [Address] = @Address, [Postcode] = @Postcode, [Mobile_Number] = @Mobile_Number, [Email] = @Email, [Rental_Number] = @Rental_Number
WHERE (([UserID] = @UserID) OR ([UserID] IS NULL AND @UserID IS NULL) OR ([UserName] IS NULL AND @UserName IS NULL))">






























































I've tried removing the UserName field from updating but it then leaves the field blank and messes it up, can anyone please help me out?



Simply set the BoundField readonly property to true, then remove the field from update command (set fieldname =...)

I've tried that before, but it doesn't work when i edit and click update it doesn't do anything?



When using access database you need to count the "?", the first update parameter is for first "?" and soon until the last parameter for last "?". Match the "?" and parameter by the order. I suggest you using single parameter as WHERE statement. When you remove
one "?", you need to remove the parameter also, if not you will pass wrong parameter value. This behavior not happen in SqlDataSource because you able to give parameter name like @Userid, and don't worry about the order in SqlDataSource.

MY WHERE statements is UserName = session, Look all i want to do is leave the UserName value as default and update everything else except that field, I don;t know what im doing wrong



Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Dont worry I fixed it through the AccessDataSource, cheers anyway


[RESOLVED] DetailsView to stop updating a certain field


So i have a database for the customers details and i have set up a DetailsView so that the customers can view their details and edit them and update them. It updates but the problem is that it gets the users details via the UserName, I set the UserName property
to not be able to edit it as i want to leave it as default, but when i update it updates everything except the UserName, so all i can do is just leave it to show to UserName property but i dont want that because i dont want the user to edit his username. Heres
the code to it:



DataFile="~/App_Data/ASPNetDB.mdb"

InsertCommand="INSERT INTO [tbl_Customer] ([UserID], [Title], [UserName], [Firstname], [Surname], [Address], [Postcode], [Mobile_Number], [Email], [Rental_Number]) VALUES (@UserID, @Title, @UserName, @Firstname, @Surname, @Address, @Postcode, @Mobile_Number,
@Email, @Rental_Number)"

SelectCommand="SELECT * FROM [tbl_Customer] WHERE ([UserName] = )"

UpdateCommand="UPDATE [tbl_Customer] SET [Title] = @Title, [UserName] = @UserName, [Firstname] = @Firstname, [Surname] = @Surname, [Address] = @Address, [Postcode] = @Postcode, [Mobile_Number] = @Mobile_Number, [Email] = @Email, [Rental_Number] = @Rental_Number
WHERE (([UserID] = @UserID) OR ([UserID] IS NULL AND @UserID IS NULL) OR ([UserName] IS NULL AND @UserName IS NULL))">






























































I've tried removing the UserName field from updating but it then leaves the field blank and messes it up, can anyone please help me out?



Simply set the BoundField readonly property to true, then remove the field from update command (set fieldname =...)

I've tried that before, but it doesn't work when i edit and click update it doesn't do anything?



When using access database you need to count the "?", the first update parameter is for first "?" and soon until the last parameter for last "?". Match the "?" and parameter by the order. I suggest you using single parameter as WHERE statement. When you remove
one "?", you need to remove the parameter also, if not you will pass wrong parameter value. This behavior not happen in SqlDataSource because you able to give parameter name like @Userid, and don't worry about the order in SqlDataSource.

MY WHERE statements is UserName = session, Look all i want to do is leave the UserName value as default and update everything else except that field, I don;t know what im doing wrong



Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Dont worry I fixed it through the AccessDataSource, cheers anyway


[RESOLVED] DetailsView to stop updating a certain field


So i have a database for the customers details and i have set up a DetailsView so that the customers can view their details and edit them and update them. It updates but the problem is that it gets the users details via the UserName, I set the UserName property
to not be able to edit it as i want to leave it as default, but when i update it updates everything except the UserName, so all i can do is just leave it to show to UserName property but i dont want that because i dont want the user to edit his username. Heres
the code to it:



DataFile="~/App_Data/ASPNetDB.mdb"

InsertCommand="INSERT INTO [tbl_Customer] ([UserID], [Title], [UserName], [Firstname], [Surname], [Address], [Postcode], [Mobile_Number], [Email], [Rental_Number]) VALUES (@UserID, @Title, @UserName, @Firstname, @Surname, @Address, @Postcode, @Mobile_Number,
@Email, @Rental_Number)"

SelectCommand="SELECT * FROM [tbl_Customer] WHERE ([UserName] = )"

UpdateCommand="UPDATE [tbl_Customer] SET [Title] = @Title, [UserName] = @UserName, [Firstname] = @Firstname, [Surname] = @Surname, [Address] = @Address, [Postcode] = @Postcode, [Mobile_Number] = @Mobile_Number, [Email] = @Email, [Rental_Number] = @Rental_Number
WHERE (([UserID] = @UserID) OR ([UserID] IS NULL AND @UserID IS NULL) OR ([UserName] IS NULL AND @UserName IS NULL))">






























































I've tried removing the UserName field from updating but it then leaves the field blank and messes it up, can anyone please help me out?



Simply set the BoundField readonly property to true, then remove the field from update command (set fieldname =...)

I've tried that before, but it doesn't work when i edit and click update it doesn't do anything?



When using access database you need to count the "?", the first update parameter is for first "?" and soon until the last parameter for last "?". Match the "?" and parameter by the order. I suggest you using single parameter as WHERE statement. When you remove
one "?", you need to remove the parameter also, if not you will pass wrong parameter value. This behavior not happen in SqlDataSource because you able to give parameter name like @Userid, and don't worry about the order in SqlDataSource.

MY WHERE statements is UserName = session, Look all i want to do is leave the UserName value as default and update everything else except that field, I don;t know what im doing wrong



Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Dont worry I fixed it through the AccessDataSource, cheers anyway


[RESOLVED] DetailsView to stop updating a certain field


So i have a database for the customers details and i have set up a DetailsView so that the customers can view their details and edit them and update them. It updates but the problem is that it gets the users details via the UserName, I set the UserName property
to not be able to edit it as i want to leave it as default, but when i update it updates everything except the UserName, so all i can do is just leave it to show to UserName property but i dont want that because i dont want the user to edit his username. Heres
the code to it:



DataFile="~/App_Data/ASPNetDB.mdb"

InsertCommand="INSERT INTO [tbl_Customer] ([UserID], [Title], [UserName], [Firstname], [Surname], [Address], [Postcode], [Mobile_Number], [Email], [Rental_Number]) VALUES (@UserID, @Title, @UserName, @Firstname, @Surname, @Address, @Postcode, @Mobile_Number,
@Email, @Rental_Number)"

SelectCommand="SELECT * FROM [tbl_Customer] WHERE ([UserName] = )"

UpdateCommand="UPDATE [tbl_Customer] SET [Title] = @Title, [UserName] = @UserName, [Firstname] = @Firstname, [Surname] = @Surname, [Address] = @Address, [Postcode] = @Postcode, [Mobile_Number] = @Mobile_Number, [Email] = @Email, [Rental_Number] = @Rental_Number
WHERE (([UserID] = @UserID) OR ([UserID] IS NULL AND @UserID IS NULL) OR ([UserName] IS NULL AND @UserName IS NULL))">






























































I've tried removing the UserName field from updating but it then leaves the field blank and messes it up, can anyone please help me out?



Simply set the BoundField readonly property to true, then remove the field from update command (set fieldname =...)

I've tried that before, but it doesn't work when i edit and click update it doesn't do anything?



When using access database you need to count the "?", the first update parameter is for first "?" and soon until the last parameter for last "?". Match the "?" and parameter by the order. I suggest you using single parameter as WHERE statement. When you remove
one "?", you need to remove the parameter also, if not you will pass wrong parameter value. This behavior not happen in SqlDataSource because you able to give parameter name like @Userid, and don't worry about the order in SqlDataSource.

MY WHERE statements is UserName = session, Look all i want to do is leave the UserName value as default and update everything else except that field, I don;t know what im doing wrong



Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Afaik, parameter name in update/insert/delete command actually not work in AccessDataSource. Concern to the order. First parameter (whatever name) in update command get value from first update parameter.

Dont worry I fixed it through the AccessDataSource, cheers anyway


[RESOLVED] Field value in gridview


How can i get the checked field value of Grid, in Textbox.



i could not get it properly, can you explain it bit more, specially what is the link between "checked field" and textbox. is there a check-box for every textbox in grid? or something else.



aspx file:
===============



Untitled Page
















DataSourceid="AccessDataSource1"


style="z-index: 100; left: 7px; position: absolute; top: 17px; height: 177px; width: 429px;"
CellPadding="4" ForeColor="#333333" GridLines="None">












































onclick="Button_Click" />


id="AccessDataSource1"
DataFile="~/App_Data/Contacts.mdb"
runat="server"
SelectCommand="SELECT * FROM student">










                         




                  




                      




       




    










There is a grid, that has checkbox column..


if i check one checkbox then the data of that row will be displayed in textboxes (these textboxes are not in grid, they are shown below grid)


the data of particular checked row is displayed on button click , like ( texbox1 has column1 of grid's value , textbox2 has column2 value and so on, till text box5)




Hi,


Try this in button click event


foreach(GridViewRow  gvrow in gridview1.Rows)

{

CheckBox chk = (CheckBox)gvrow.FindControl("c");

if (chk != null & chk.Checked)

{

str += gridview1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';

strname += gvrow.Cells[2].Text+',';
......// write code to set textbox value
}

}

And refer the blog


http://www.aspdotnet-suresh.com/2013/03/get-gridview-row-values-when-checkbox.html


Hope it can help u. 


[RESOLVED] Error delete and edit in gridview


i am writing a code below which is used in my gridview (asp.net+vb web with access database)


                    ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>"

SelectCommand="SELECT * FROM [Tasks]"
DeleteCommand="delete [Tasks] where id=@id"

UpdateCommand="update [Tasks] set TaskDescription=@TaskDescription,StartDate=StartDate,EndDate=@EndDate,Notes=@Notes where id=@id"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>">












when i click edit button in my gridview and ammend something and press update the data is not updated


and the delete button gives eror as


Syntax error (missing operator) in query expression '[Tasks] where id=@id'.








I think your Update "parameters" section needs to include the "ID" variable, as in;









I don't see anything in the Delete mechanism that could be causing an error, so maybe the missing ID in the Update is failing over to the Delete event.



But, try repairing the Update Parameter, and see if that helps.





added ID in update parameter still not updating



Did you really want StartDate=StartDate in your UpdateCommand?  Also, if I recall correctly, use a ? instead of @ to pass parameters in Access, so try:


UpdateCommand="update  [Tasks] set TaskDescription = ?, StartDate = ?, EndDate= ?, Notes = ? where id = ?"


 



Also, why are you using an SQLDataSource, instead of an AccessDataSource?


  DataFile="~/myAccessDatabase.mdb"
SelectCommand="SELECT * FROM [Tasks]"
DeleteCommand="delete [Tasks] where id=?"
UpdateCommand="update [Tasks] set TaskDescription = ?, StartDate = ?, EndDate= ?, Notes = ? where id = ?"













i changed as per ur direction


edit update works fine but delete gives following error


Syntax error (missing operator) in query expression '[Tasks] where id=?'.






Try:


DeleteCommand="delete FROM [Tasks] where id = ?"

I'm not sure if you need the *, like delete * FROM [Tasks] where id = ?


 


[RESOLVED] Gridview data view


in my web page there is text boxes to insert data to database and a gridview below that to view the insertde data. the data gets inserted correctly but the data is not immediately viewed in the gridview it needs refresh page or reload how can it be rectified



Make sure you are calling GridView.DataBind() after you insert the data and update the GridView.DataSource.



On the same event you have for inserting data you need to rebind your grid.


So if you have


cmdSave_Click{


 // code o add data



gvGridviewName.DataSource = dsName;

gvGridviewName.DataBind(); 


}



Make sure you're binding the grid at the right time/event. 



i am using sqldatasource with select command in .aspx page and wrote the code like this after  .vb code page 


 GridView1.DataBind()


still that last entry is not viewed in the grid the second last entry is shown


when enter one more row then the previous data is viewed



Try calling SqlDataSource1.DataBind() before GridView1.DataBind().  Also make sure  EnableCaching = "False" on the SqlDataSource control.



Call the BindGridView() function as the last statement in the  save button click.


For reference check the article Bind,Save,Edit,Update,Cancel,Delete,Paging
example in GridView in asp.net C#


if you are using VB language then read How
to Bind,Insert,Edit,Update,Delete in GridView in asp.net VB.Net





not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Baiju EP



not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Could you post the code behind here where you insert the new data and databind, so we can see the actual code calls?



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDbConnection
con.ConnectionString = ConfigurationManager.ConnectionStrings("baijuepConnectionString").ConnectionString
con.Open()
Dim oledb_ins As String
oledb_ins = "insert into Tasks (TaskDescription, startdate, enddate, Notes) values (@a,@b,@c,@d)"
Dim oledbcom As New OleDbCommand(oledb_ins, con)
oledbcom.Parameters.Add(New OleDbParameter("@a", OleDbType.VarChar, 50)).Value = tasktxt.Text
oledbcom.Parameters.Add(New OleDbParameter("@b", OleDbType.VarChar, 50)).Value = startdt.Text
oledbcom.Parameters.Add(New OleDbParameter("@c", OleDbType.VarChar, 50)).Value = enddt.Text
oledbcom.Parameters.Add(New OleDbParameter("@d", OleDbType.VarChar, 50)).Value = notestxt.Text
oledbcom.ExecuteNonQuery()
tasktxt.Text = ""
startdt.Text = ""
enddt.Text = ""
notestxt.Text = ""
SqlDataSource1.DataBind()
GridView1.DataBind()
Page.ClientScript.RegisterStartupScript(Me.GetType, "Forms", "")

ens usb

sqldatasource


                    ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>" 

SelectCommand="SELECT * FROM [Taskss]"
DeleteCommand="delete [Tasks] where id=@id"

UpdateCommand="update [Tasks] set TaskDescription=@TaskDescription,StartDate=StartDate,EndDate=@EndDate,Notes=@Notes where id=@id"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>">













the data get inserted into the database but not displying in the gridview at once


it gets viewed when one more entry is made how can it be possible


and with the edit/delete in grid it is also not working





i was not able to solve the problem



I noticed in your SqlDataSource you have


 SelectCommand="SELECT * FROM [Taskss]" 

it should be


SelectCommand="SELECT * FROM [Tasks]"

Other than that it should work. If that was just a copy paste typo into this forum and it is [Tasks] in your actual code already then maybe it's some kind of screen refresh issue. Are you using an Ajax UpdatePanel or something?  After you click the button
and the record is not there does it show up if you hit ctrl-F5?


For Gridview edit make sure you have DataKeyNames="id" in your GridView.




I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





Baiju EP



I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





When you add a new record and the previous record gets displayed, does the new record show if you press ctrl+F5 in your browser?



yes when i add a record and press refresh the dtata gets displayed



In your markup are you using Ajax or an UpdatePanel?  If so make sure your insert Button and GridView are in the same UpdatePanel or Ajax call.



i am using accessdatasource to all actions. select, insert,update,delete code all are in accessdatasource



Hi Baiju EP,


Based on your description I have created a sample,  I find it can work fine, I also find a mistake in your code:

the “SelectCommand="SELECT * FROM [Taskss]"”  T-SQL statement which select data from “Taskss”, but in behind code, you have insert data into “Tasks”, so please check you table name again.


The following is my sample code, please try to refer to it.


In the .aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewDataView.WebForm1" %>






















ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>"

SelectCommand="SELECT [Id], [name], [age], [sex] FROM [Student]"

UpdateCommand="Update Student set name = 'Mr. OK'"

DeleteCommand="Delete Student">





























In the .aspx.cs:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;



namespace GridViewDataView

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}



protected void Insert_Click(object sender, EventArgs e)

{

string connStr = ConfigurationManager.ConnectionStrings["DemoDBConnectionString"].ToString();

SqlCommand cmd = new SqlCommand("insert into Student values(@Id,@name,@age,@sex)", new SqlConnection(connStr));

SqlParameter[] parameters = new SqlParameter[] {

new SqlParameter("@Id",5),

new SqlParameter("@name","Andy"),

new SqlParameter("@age",24),

new SqlParameter("@sex","male")

};

foreach (SqlParameter p in parameters)

{

cmd.Parameters.Add(p);

}

if (cmd.Connection.State == System.Data.ConnectionState.Closed)

{

cmd.Connection.Open();

}

if (cmd.ExecuteNonQuery() > 0)

{

SqlDataSource2.DataBind();

GridView1.DataBind();

Page.ClientScript.RegisterStartupScript(this.GetType(), "Forms", "");

}

if (cmd.Connection.State == System.Data.ConnectionState.Open)

{

cmd.Connection.Close();

}

}

}

}

Best Regards,

Terry Guo


[RESOLVED] Gridview data view


in my web page there is text boxes to insert data to database and a gridview below that to view the insertde data. the data gets inserted correctly but the data is not immediately viewed in the gridview it needs refresh page or reload how can it be rectified



Make sure you are calling GridView.DataBind() after you insert the data and update the GridView.DataSource.



On the same event you have for inserting data you need to rebind your grid.


So if you have


cmdSave_Click{


 // code o add data



gvGridviewName.DataSource = dsName;

gvGridviewName.DataBind(); 


}



Make sure you're binding the grid at the right time/event. 



i am using sqldatasource with select command in .aspx page and wrote the code like this after  .vb code page 


 GridView1.DataBind()


still that last entry is not viewed in the grid the second last entry is shown


when enter one more row then the previous data is viewed



Try calling SqlDataSource1.DataBind() before GridView1.DataBind().  Also make sure  EnableCaching = "False" on the SqlDataSource control.



Call the BindGridView() function as the last statement in the  save button click.


For reference check the article Bind,Save,Edit,Update,Cancel,Delete,Paging
example in GridView in asp.net C#


if you are using VB language then read How
to Bind,Insert,Edit,Update,Delete in GridView in asp.net VB.Net





not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Baiju EP



not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Could you post the code behind here where you insert the new data and databind, so we can see the actual code calls?



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDbConnection
con.ConnectionString = ConfigurationManager.ConnectionStrings("baijuepConnectionString").ConnectionString
con.Open()
Dim oledb_ins As String
oledb_ins = "insert into Tasks (TaskDescription, startdate, enddate, Notes) values (@a,@b,@c,@d)"
Dim oledbcom As New OleDbCommand(oledb_ins, con)
oledbcom.Parameters.Add(New OleDbParameter("@a", OleDbType.VarChar, 50)).Value = tasktxt.Text
oledbcom.Parameters.Add(New OleDbParameter("@b", OleDbType.VarChar, 50)).Value = startdt.Text
oledbcom.Parameters.Add(New OleDbParameter("@c", OleDbType.VarChar, 50)).Value = enddt.Text
oledbcom.Parameters.Add(New OleDbParameter("@d", OleDbType.VarChar, 50)).Value = notestxt.Text
oledbcom.ExecuteNonQuery()
tasktxt.Text = ""
startdt.Text = ""
enddt.Text = ""
notestxt.Text = ""
SqlDataSource1.DataBind()
GridView1.DataBind()
Page.ClientScript.RegisterStartupScript(Me.GetType, "Forms", "")

ens usb

sqldatasource


                    ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>" 

SelectCommand="SELECT * FROM [Taskss]"
DeleteCommand="delete [Tasks] where id=@id"

UpdateCommand="update [Tasks] set TaskDescription=@TaskDescription,StartDate=StartDate,EndDate=@EndDate,Notes=@Notes where id=@id"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>">













the data get inserted into the database but not displying in the gridview at once


it gets viewed when one more entry is made how can it be possible


and with the edit/delete in grid it is also not working





i was not able to solve the problem



I noticed in your SqlDataSource you have


 SelectCommand="SELECT * FROM [Taskss]" 

it should be


SelectCommand="SELECT * FROM [Tasks]"

Other than that it should work. If that was just a copy paste typo into this forum and it is [Tasks] in your actual code already then maybe it's some kind of screen refresh issue. Are you using an Ajax UpdatePanel or something?  After you click the button
and the record is not there does it show up if you hit ctrl-F5?


For Gridview edit make sure you have DataKeyNames="id" in your GridView.




I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





Baiju EP



I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





When you add a new record and the previous record gets displayed, does the new record show if you press ctrl+F5 in your browser?



yes when i add a record and press refresh the dtata gets displayed



In your markup are you using Ajax or an UpdatePanel?  If so make sure your insert Button and GridView are in the same UpdatePanel or Ajax call.



i am using accessdatasource to all actions. select, insert,update,delete code all are in accessdatasource



Hi Baiju EP,


Based on your description I have created a sample,  I find it can work fine, I also find a mistake in your code:

the “SelectCommand="SELECT * FROM [Taskss]"”  T-SQL statement which select data from “Taskss”, but in behind code, you have insert data into “Tasks”, so please check you table name again.


The following is my sample code, please try to refer to it.


In the .aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewDataView.WebForm1" %>






















ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>"

SelectCommand="SELECT [Id], [name], [age], [sex] FROM [Student]"

UpdateCommand="Update Student set name = 'Mr. OK'"

DeleteCommand="Delete Student">





























In the .aspx.cs:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;



namespace GridViewDataView

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}



protected void Insert_Click(object sender, EventArgs e)

{

string connStr = ConfigurationManager.ConnectionStrings["DemoDBConnectionString"].ToString();

SqlCommand cmd = new SqlCommand("insert into Student values(@Id,@name,@age,@sex)", new SqlConnection(connStr));

SqlParameter[] parameters = new SqlParameter[] {

new SqlParameter("@Id",5),

new SqlParameter("@name","Andy"),

new SqlParameter("@age",24),

new SqlParameter("@sex","male")

};

foreach (SqlParameter p in parameters)

{

cmd.Parameters.Add(p);

}

if (cmd.Connection.State == System.Data.ConnectionState.Closed)

{

cmd.Connection.Open();

}

if (cmd.ExecuteNonQuery() > 0)

{

SqlDataSource2.DataBind();

GridView1.DataBind();

Page.ClientScript.RegisterStartupScript(this.GetType(), "Forms", "");

}

if (cmd.Connection.State == System.Data.ConnectionState.Open)

{

cmd.Connection.Close();

}

}

}

}

Best Regards,

Terry Guo


[RESOLVED] Gridview data view


in my web page there is text boxes to insert data to database and a gridview below that to view the insertde data. the data gets inserted correctly but the data is not immediately viewed in the gridview it needs refresh page or reload how can it be rectified



Make sure you are calling GridView.DataBind() after you insert the data and update the GridView.DataSource.



On the same event you have for inserting data you need to rebind your grid.


So if you have


cmdSave_Click{


 // code o add data



gvGridviewName.DataSource = dsName;

gvGridviewName.DataBind(); 


}



Make sure you're binding the grid at the right time/event. 



i am using sqldatasource with select command in .aspx page and wrote the code like this after  .vb code page 


 GridView1.DataBind()


still that last entry is not viewed in the grid the second last entry is shown


when enter one more row then the previous data is viewed



Try calling SqlDataSource1.DataBind() before GridView1.DataBind().  Also make sure  EnableCaching = "False" on the SqlDataSource control.



Call the BindGridView() function as the last statement in the  save button click.


For reference check the article Bind,Save,Edit,Update,Cancel,Delete,Paging
example in GridView in asp.net C#


if you are using VB language then read How
to Bind,Insert,Edit,Update,Delete in GridView in asp.net VB.Net





not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Baiju EP



not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Could you post the code behind here where you insert the new data and databind, so we can see the actual code calls?



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDbConnection
con.ConnectionString = ConfigurationManager.ConnectionStrings("baijuepConnectionString").ConnectionString
con.Open()
Dim oledb_ins As String
oledb_ins = "insert into Tasks (TaskDescription, startdate, enddate, Notes) values (@a,@b,@c,@d)"
Dim oledbcom As New OleDbCommand(oledb_ins, con)
oledbcom.Parameters.Add(New OleDbParameter("@a", OleDbType.VarChar, 50)).Value = tasktxt.Text
oledbcom.Parameters.Add(New OleDbParameter("@b", OleDbType.VarChar, 50)).Value = startdt.Text
oledbcom.Parameters.Add(New OleDbParameter("@c", OleDbType.VarChar, 50)).Value = enddt.Text
oledbcom.Parameters.Add(New OleDbParameter("@d", OleDbType.VarChar, 50)).Value = notestxt.Text
oledbcom.ExecuteNonQuery()
tasktxt.Text = ""
startdt.Text = ""
enddt.Text = ""
notestxt.Text = ""
SqlDataSource1.DataBind()
GridView1.DataBind()
Page.ClientScript.RegisterStartupScript(Me.GetType, "Forms", "")

ens usb

sqldatasource


                    ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>" 

SelectCommand="SELECT * FROM [Taskss]"
DeleteCommand="delete [Tasks] where id=@id"

UpdateCommand="update [Tasks] set TaskDescription=@TaskDescription,StartDate=StartDate,EndDate=@EndDate,Notes=@Notes where id=@id"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>">













the data get inserted into the database but not displying in the gridview at once


it gets viewed when one more entry is made how can it be possible


and with the edit/delete in grid it is also not working





i was not able to solve the problem



I noticed in your SqlDataSource you have


 SelectCommand="SELECT * FROM [Taskss]" 

it should be


SelectCommand="SELECT * FROM [Tasks]"

Other than that it should work. If that was just a copy paste typo into this forum and it is [Tasks] in your actual code already then maybe it's some kind of screen refresh issue. Are you using an Ajax UpdatePanel or something?  After you click the button
and the record is not there does it show up if you hit ctrl-F5?


For Gridview edit make sure you have DataKeyNames="id" in your GridView.




I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





Baiju EP



I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





When you add a new record and the previous record gets displayed, does the new record show if you press ctrl+F5 in your browser?



yes when i add a record and press refresh the dtata gets displayed



In your markup are you using Ajax or an UpdatePanel?  If so make sure your insert Button and GridView are in the same UpdatePanel or Ajax call.



i am using accessdatasource to all actions. select, insert,update,delete code all are in accessdatasource



Hi Baiju EP,


Based on your description I have created a sample,  I find it can work fine, I also find a mistake in your code:

the “SelectCommand="SELECT * FROM [Taskss]"”  T-SQL statement which select data from “Taskss”, but in behind code, you have insert data into “Tasks”, so please check you table name again.


The following is my sample code, please try to refer to it.


In the .aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewDataView.WebForm1" %>






















ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>"

SelectCommand="SELECT [Id], [name], [age], [sex] FROM [Student]"

UpdateCommand="Update Student set name = 'Mr. OK'"

DeleteCommand="Delete Student">





























In the .aspx.cs:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;



namespace GridViewDataView

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}



protected void Insert_Click(object sender, EventArgs e)

{

string connStr = ConfigurationManager.ConnectionStrings["DemoDBConnectionString"].ToString();

SqlCommand cmd = new SqlCommand("insert into Student values(@Id,@name,@age,@sex)", new SqlConnection(connStr));

SqlParameter[] parameters = new SqlParameter[] {

new SqlParameter("@Id",5),

new SqlParameter("@name","Andy"),

new SqlParameter("@age",24),

new SqlParameter("@sex","male")

};

foreach (SqlParameter p in parameters)

{

cmd.Parameters.Add(p);

}

if (cmd.Connection.State == System.Data.ConnectionState.Closed)

{

cmd.Connection.Open();

}

if (cmd.ExecuteNonQuery() > 0)

{

SqlDataSource2.DataBind();

GridView1.DataBind();

Page.ClientScript.RegisterStartupScript(this.GetType(), "Forms", "");

}

if (cmd.Connection.State == System.Data.ConnectionState.Open)

{

cmd.Connection.Close();

}

}

}

}

Best Regards,

Terry Guo