 |
|
 |
I noticed the insert query was not firing, so I made a quick and dirty mod of the original sub, Replace the gridview1_rowadding sub with the following, change sqlstr variable to your schema Basicaly it just loops throught the hashtable and forms a query to execute. Sorry about the vb! add a label txtError to see whats going on...Hope this helps someone out!
Protected Sub GridView1_RowAdding(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
If e.RowIndex > 0 Then
Return
End If
Dim h As New System.Collections.Hashtable()
Dim Item As DictionaryEntry
For Each x As System.Collections.DictionaryEntry In e.NewValues
h(x.Key) = x.Value
Next
Dim sqlstr As String = "INSERT INTO [mytable] (col3, col2, col1) values ("
For Each Item In h
sqlstr = sqlstr & "'" & Item.Value & "',"
Next
sqlstr = Left(sqlstr, Len(sqlstr) - 1)
sqlstr = sqlstr & ")"
Dim myConnection As New System.Data.SqlClient.SqlConnection(ConnStr)
Try
myConnection.Open()
Dim command As New System.Data.SqlClient.SqlCommand(sqlstr, myConnection)
command.ExecuteNonQuery()
myConnection.Close()
txtError.Text = "<br />Row has been added."
Catch ex As Exception
txtError.Text = "An error occured:" & ex.ToString() & " sql:" & sqlstr
Finally
myConnection.Close()
End Try
End Sub
|
|
|
|
 |
|
 |
Thank you. This solution works for me in Internet Explorer only. For some reason, it does not work in Firefox, Edit remains Edit.
Anyway, I have added AllowPaging to my gridview, as there are currently nearly 70000 records in my database. On page 1, all is fine. But go to page 2, the top line is saying Add, but there is a record there.
I don't know what is best here. Do I switch off the Fixgrid routine if the GridView.PageIndex is higher than 0? In which case, the user can only insert if he is on GridView.PageIndex 0.
Firstly, I don't know how to do that.
Secondly, is this really the most elegant solution?
|
|
|
|
 |
|
 |
I am still trying to get this to work in Firefox.
First, it did not like document.all. Replaced that line with:
var grid=document.getElementById('ctl00_ContentPlaceHolder1_gvFieldReturns');
FixGrid(grid);
Now, it says idGrid.firstChild.childNode[1] is undefined.
This is the line in question:
var Parts = idGrid.firstChild.childNodes[1].childNodes[0].innerHTML.split(">Edit<");
|
|
|
|
 |
|
 |
The method below corrects this:Corrected javascript for IE, FF, and Safari
|
|
|
|
 |
|
 |
Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.
Cooooooooooool !
|
|
|
|
 |
|
 |
I want to display blank GridView with one row and 5 columns on the UI for that the user can insert value into Database with that grid.
|
|
|
|
 |
|
 |
If you use an update stored procedure to be smart enough for updating and inserting a record, then you don't need to catch the GridView1_RowAdding event.
|
|
|
|
 |
|
 |
If you're using the boring GridView control, this javascript may work a lil better for you.
function AlterFirstEditToAdd(GridViewClientID)
{
if (GridViewClientID != null && GridViewClientID != '')
{
try
{
var gridView = null;
var parts = null;
var tmp = null;
var html = null;
var firstRowNode = 1;
var isFirstPage = true;
gridView = document.getElementById(GridViewClientID);
html = gridView.firstChild.childNodes[0].childNodes[0].innerHTML;
if (html.indexOf('Page$1') > -1)
{
isFirstPage = false;
}
if (isFirstPage == true)
{
html = gridView.firstChild.childNodes[firstRowNode].childNodes[0].innerHTML;
if (html.indexOf('Edit$0') == -1 )
{
firstRowNode = firstRowNode + 1;
}
html = gridView.firstChild.childNodes[firstRowNode].childNodes[0].innerHTML;
parts = html.split(">Edit<");
tmp = parts.join(">Add<");
parts = tmp.split(">Delete<");
gridView.firstChild.childNodes[firstRowNode].childNodes[0].innerHTML = parts.join("><");
}
}
catch (e)
{
}
}
}
|
|
|
|
 |
|
 |
how to update and add in gridview using stored procedures
|
|
|
|
 |
|
 |
The javascript is different in IE and FF/Safari. if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var Parts = idGrid.lastChild.childNodes[1].childNodes[1].innerHTML.split(">Edit<"); var tmp = Parts.join(">Add<"); Parts = tmp.split(">Delete<"); idGrid.lastChild.childNodes[1].childNodes[1].innerHTML = Parts.join("><"); } else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { var Parts = idGrid.lastChild.childNodes[1].childNodes[0].innerHTML.split(">Edit<"); var tmp = Parts.join(">Add<"); Parts = tmp.split(">Delete<"); idGrid.lastChild.childNodes[1].childNodes[0].innerHTML = Parts.join("><"); } else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { var Parts = idGrid.lastChild.childNodes[1].childNodes[1].innerHTML.split(">Edit<"); var tmp = Parts.join(">Add<"); Parts = tmp.split(">Delete<"); idGrid.lastChild.childNodes[1].childNodes[1].innerHTML = Parts.join("><"); }
|
|
|
|
 |
|
 |
Hello,
Can any one please provide the code for completing this task of adding the records, ie. code for inserting data in hashtable through data access layer. It would be a great help.
Thanks,
Anadi Singh
|
|
|
|
 |
|
 |
Hi,
i am biggner to ASP.Net using C# please help me out .How to place data from SQLSERVER into gridview and then edit and delete data and also after deleting and updating data from gridview and store updated data again in database
|
|
|
|
 |
|
 |
Nice submission, cheers.
I wanted the 'insert' line at the end of the list, rather than the first line, so instead of using the view_updating event used the datasource_updating instead.
An attempt to 'update' a record with a primary key of 0, causes an insert to be executed - the update parameters are copied into the insert parameter collection (minus the leading '@') so it is quite self contained.
There appears to be no way of chekcing how many parameters there are, so I have (rather naffly) just trapped the error when it tries to read past the end of the array.
I leave the update to fail quitely - however I guess it could be cancelled.
Hope its of use to someone.
Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating
If e.Command.Parameters.Item("@id").Value = 0 Then
SqlDataSource1.InsertParameters.Clear()
Dim i As Integer = 0
Try
Do
SqlDataSource1.InsertParameters.Add(e.Command.Parameters.Item(i).ParameterName.Remove(0, 1), e.Command.Parameters.Item(i).Value)
i += 1
Loop
Catch ex As Exception
End Try
SqlDataSource1.InsertParameters.Add("page", Request.QueryString("id"))
SqlDataSource1.Insert()
End If
End Sub
Paul /)/+)
|
|
|
|
 |
|
 |
I got the code to work, and its awesome!
However, when I sort or page, the data in the grid replaces the first "Add" row with the appropriate data...
Anyone have a fix for this?
|
|
|
|
 |
|
 |
This worked very well for me. I used it in a page with seven GridViews that are not visible all the time, so here is what I did to make it work:
1- Repeated the lines below for as many data grids I wanted to apply the code to:
<script>
FixGrid(document.all.GridView1);
FixGrid(document.all.GridView2);
FixGrid(document.all.GridView...n);
</script>
2-Added an "if" statement to the FixGrid(idGrid) method to make sure it will not produce the error "firstChild is null or not an object" when one of the GridViews are not visible:
<script>
function FixGrid(idGrid)
{
// content looks like:
//"Edit
// Delete"
// replace Edit with Add, remove Delete
if (idGrid)
{
var Parts = idGrid.firstChild.childNodes[1].childNodes[0].innerHTML.split(">Edit<");
var tmp = Parts.join(">Add<");
Parts = tmp.split(">Delete<");
Parts = tmp.split(">Select<");
idGrid.firstChild.childNodes[1].childNodes[0].innerHTML = Parts.join("><");
}
}
</script>
HamidTheProgrammer
|
|
|
|
 |
|
 |
I tried to use your code exactly as it was written, and when I enter a new
record in line 0 and press update, it goes to the function of ROWADDING
do what it suppose to do but nothing is entered into the table. Have any idea why?
THanks
mohandasclt@gmail.com
|
|
|
|
 |
|
 |
Had problems calling the Javascript and Master Page. I did it this way in the code. My GridView name is grdUser and my ID for the ContentPlaceHolder is "CM" Also be careful on ctl00. It might be different based on a web config setting but that's a whole other topic. Just mouse over your Edits and see how they are named.
Protected Sub grdUser_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowIndex = 0 And e.Row.RowState <> DataControlRowState.Edit Then e.Row.Cells(0).Text = "<a href=""javascript:__doPostBack('ctl00$CM$grdUser','Edit$0')"">Add</a>" End If End Sub
And this is my SLOP code for inserting Dim i As Integer = 0 For Each x As System.Collections.DictionaryEntry In e.NewValues 'h(x.Key) = x.Value If i = 0 Then Title = x.Value If i = 1 Then FirstName = x.Value If i = 2 Then LastName = x.Value If i = 3 Then Email = x.Value If i = 4 Then Admin = x.Value If i = 5 Then Active = x.Value i += 1 Next If Title <> "" And FirstName <> "" And LastName <> "" And Email <> "" Then With SqlDataSource1 .InsertParameters("usrTitle").DefaultValue = Title .InsertParameters("usrFirstname").DefaultValue = FirstName .InsertParameters("usrLastName").DefaultValue = LastName .InsertParameters("usrEmail").DefaultValue = Email .InsertParameters("TrainingAdmin").DefaultValue = Admin .InsertParameters("Active").DefaultValue = Active .Insert() End With End If
-- modified at 12:22 Wednesday 15th November, 2006
|
|
|
|
 |
|
 |
I'm using an image instead of text for Edit, Cancel, etc. and ran into some problems using the Javascript version. After the row was added the Edit image wasn't updated to the Add image. I'm using Atlas which I don't believe would cause the new issue, but wanted to provide as much info as possible. I'm now attempting to perform the image switch in code behind. The first row image is replaced successfully but when I click on it to enter Edit mode I receive an error - Invalid postback or callback argument. Blah, blah, blah. Can someone see what I may be doing wrong?
Protected Sub grdUsers_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdUsers.RowDataBound
If e.Row.RowIndex = 0 And e.Row.RowState <> DataControlRowState.Edit Then
e.Row.Cells(1).Text = "<input type=""image"" src=""Images/add.gif"" alt=""Add"" önclick=""javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdUsers','Edit$0')"" style=""border-width:0px;"" />"
End If
End Sub
|
|
|
|
 |
|
 |
Hi,
It's very good. But, how will be the situation, if we use sorting and paging for the same grid? If we sort the grid by a column, the empty row at the first line which we use for adding the new one will be populated with some other record.How can we overcome that??
-Priya
|
|
|
|
 |
|
 |
Hey,
for those of you who had the same problem with changing the name for the linkbuttons here is the fix:
var Parts = idGrid.childNodes[1].childNodes[1].innerHTML.split(">Edit<");
var tmp = Parts.join(">Add<");
Parts = tmp.split(">Delete<");
idGrid.childNodes[1].childNodes[1].innerHTML = Parts.join("><");
|
|
|
|
 |
|
 |
Hi
I tried this with SqlDataSource and it worked.
When I try this with strongly typed dataset as datasource, it does not work.
Any special setting to be done in this case?
Thanks for any help.
Nitesh
|
|
|
|
 |
|
 |
I tried to add this to my grid, but I got an error. I used to insert the data in the SqlDataSource the following code.
-------------------------------------------------
// Insert parameter TestGroupDescription
if (h["TestGroupDescription"] == null)
{
h["TestGroupDescription"] = "";
}
SqlDataSource1.InsertParameters["@TestGroupDescription"] = new Parameter(h["TestGroupDescription"].ToString());
// Insert parameter TestGroupPriority
SqlDataSource1.InsertParameters["@TestGroupPriority"] = new Parameter(h["TestGroupPriority"].ToString());
// insert values in the database
SqlDataSource1.Insert();
-------------------------------------------------
On the Insert() command I get the error that TestGroupDescription is inserted as a NULL value (and my database does not allow NULL values). Can you help me pass this exception? Thanks in advantage.
Greetings,
RBM the Spitfire
-- modified at 4:26 Tuesday 3rd October, 2006
|
|
|
|
 |
|
 |
It's working well.But, the at the new row, Edit,Delete are not being replaced by Add.
In FixGrid function, I've tried using the same code as it is in...also using document.getElementById(.....) ... but of no use.
Please help me,if anyone have worked well with this.
-Priya
|
|
|
|
 |
|
 |
Your problem is probably that the cell with the Edit Delete links is not being referenced correctly in that little javascript snippet. I know "idGrid.firstChild.childNodes[1].childNodes[0]" wasn't it for me on the first try but you'll have to do some hunting around to figure out what it is exactly (firefox + DOM Browser make it a cinch). I think the key was changing 'firstChild' to 'lastChild' or something like that.
|
|
|
|
 |
|
 |
Why don't you use the prerender event of the grid and iterate through the rows of the grid and its cells and add or remove controls.
I haven't tried for this one but have done lot of manipulation for other controls.
Try it and let us know...
--Vincent D'souza
|
|
|
|
 |