sqldatasource and a Grid delete command error - asp.net

I am using sqldatasource to binding to the grid.
SqlDatasource is like this:
<asp:SqlDataSource ID="DS_ValidUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>When I click delete, I am getting following error. I don’t have a clue where this
parameter 1 and 2 are coming from?
Microsoft JScript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException:
Command parameter[1] '' is invalid.
Command parameter[2] '' is invalid.
Thanks!

ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>

So is update working properly? ID is the primary Key right?
Try setting DataKeyName="ID" for your GV and remove the DeleteParameters from SqlDataSource.
Edit:
I would suggest you to follow this tutorial to setup your sqldatasource for Update and Delete. It could be some parameters issue. I would also suggest to handle SqlDataSource Update event and check for e.Exception to info.

Related

DATA and IMAGE are populated in two rows. I want to bind them in a single row

I'M GOING TO ASK THIS FOR THE SECOND TIME. I'M GOING TO MAKE IT CLEAR SO YOU CAN HELP ME :) Okay let's start.
I have a Registration Process. I split the process into two page. The first page is for "Personal Information" only. Then when i click the next button the next page will appear (postbackURL) This page is for "Upload Photo" page. They work fine! the data and the image are displayed on my gridview control. But my problem is here: As you can see in the figure below. The data are populated in two rows! I want them to appear in only single row! I think the problem is THE DATABASE FROM PAGE 1 IS NOT CONTINUING. THAT'S WHY WHEN I UPLOAD AN IMAGE IN PAGE 2 THE DATABASE WILL CREATE ANOTHER RECORD. hmmm...what's the problem :) I don't know how to do it. Please help!
THE DATA SHOWN IN THE TABLE BELOW ARE ONLY DUMMY. PLEASE DON'T TAKE IT SERIOUSLY.
Here's the code for INSERT/UPDATE for PersonalINFO.aspx page:
<asp:AccessDataSource runat="server" ID="AccessDataSource1" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?" DataFile="_private/records.mdb" SelectCommand="SELECT * FROM [PendingRecords]">
<DeleteParameters>
<asp:parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
<asp:parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
Here's the code for btnUpload_Click in UploadPhoto.aspx page:
<script runat="server" type="text/c#">
protected void btnUpload_Click(object sender, EventArgs e)
{
{
byte[] imageSize = new byte
[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read
(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
// Create SQL Connection
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["recordsConnectionString12"].ConnectionString;
// Create SQL Command
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO PendingRecords([Image])" + " VALUES (#Image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
OleDbParameter UploadedImage = new OleDbParameter
("#Image", OleDbType.VarBinary, imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
con.Open();
cmd.ExecuteNonQuery();
GridView1.DataSourceID = "";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
con.Close();
}
}
</script>
UploadPhoto.aspx page DATASOURCE CODE:
<asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="<%$ ConnectionStrings:recordsConnectionString12.ProviderName %>" ConnectionString="<%$ ConnectionStrings:recordsConnectionString12 %>" SelectCommand="SELECT * FROM [PendingRecords]" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
<asp:parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
You will need to pass the ID from personal info page to the Upload page (probably via query string).
And then the query for your image update should be and UPDATE query, see below.
cmd.CommandText = "UPDATE PendingRecords SET [Image] = #Image WHERE ID = #ID";
You'll now pass in the ID parameter as needed.
If the parameter is in the query string ( /UploadPhoto.aspx?id=60 )
OleDbParameter UploadedImage = new OleDbParameter
("#Image", OleDbType.VarBinary, imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
OleDbParameter IdParameter = new OleDbParameter("#ID", Request.QueryString["id"]);
cmd.Parameters.Add(IdParameter);

ASP.net, SQL database, Insert, TextBoxes

I am currently working in Visual Basic 2010 with a webform. I created an SQL database inside of Visual Basic and I'm trying to make the insert command move values from the textboxes into the database.
So my question is: how do I get the values from the textboxes (i.e:NameTextBox, AddressTextBox..) and associate them with the parameters (#Name, #Address1)?
The database works, I tested it by inputing values through the code.
Here is the relevant code:
<script runat="server">
' InsertShipper
Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
SqlDataSource1.Insert()
End Sub
</script>
<asp:Button ID="OrderButton" runat="server" onclick="InsertShipper" Text="Order" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:OrderListConnectionString %>"
DeleteCommand="DELETE FROM [ORDER] WHERE [OID] = #OID"
InsertCommand="INSERT INTO [ORDER] ([Name], [Address1], [Address2], [City], [State], [Zip], [Phone], [Email], [Subtotal], [Shipping]) VALUES ( #Name, #Address1, #Address2, #City, #State, #Zip, #Phone, #Email, #Subtotal, #Shipping)"
SelectCommand="SELECT * FROM [ORDER]"
UpdateCommand="UPDATE [ORDER] SET [Name] = #Name, [Address1] = #Address1, [Address2] = #Address2, [City] = #City, [State] = #State, [Zip] = #Zip, [Phone] = #Phone, [Email] = #Email, [Subtotal] = #Subtotal, [Shipping] = #Shipping WHERE [OID] = #OID">
<DeleteParameters>
<asp:Parameter Name="OID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter name="Name" Type="String" />
<asp:Parameter name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
<asp:Parameter Name="OID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Try setting the parameters' ControlID and PropertyName as such:
<asp:ControlParameter Name="Title"
ControlID="txtFoo"
PropertyName="Text"/>
Refer to http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
Your parameters' Name property are missing the "#" character. You need to add this or it sees it as a completely different parameter.

Cannot find SQL variable in SqlDataSource

I have the following SqlDataSource on a GridView. All works except for Delete Function.
What am I doing wrong , no error is given but no result either.
How can this be solved?
<asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$ ConnectionStrings:BookingsConnectionString %>"
OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
SelectCommand="SELECT [ID], [programme name] AS programme_name, [Start], [Finish], [Source], [Destination], [Comment] FROM [Bookings]"
DeleteCommand="DELETE FROM [Bookings] WHERE [programme name] = #ID" InsertCommand="INSERT INTO [Bookings] ([programme name], [Start], [Finish], [Source], [Destination], [Comment]) VALUES (#programme_name, #Start, #Finish, #Source, #Destination, #Comment)"
UpdateCommand="UPDATE [Bookings] SET [programme name] = #programme_name, [Start] = #Start, [Finish] = #Finish, [Source] = #Source, [Destination] = #Destination, [Comment] = #Comment WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="programme_name" Type="String" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="programme_name" Type="String" />
</UpdateParameters>
I think this:
DELETE FROM [Bookings] WHERE [programme name] = #ID
should be this:
DELETE FROM [Bookings] WHERE [ID] = #ID
You delete by [programme name] where from your other queries it looks like you want to delete by the [ID] column.
Since that columns exists, you won't get an error but also no matches to delete. Change the query to:
DELETE FROM Bookings WHERE ID=#ID

Visual Web Developer 2008 - ASP - Formview and Inserts

Im using:
Visual Web Developer 2009
Access 2007
I am trying to insert into a products table, however i keep recieveing the error:
"Data type mismatch in criteria expression."
The Edit and Delete sections work fine..
Edit Products
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="product_id" DataSourceID="AccessDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="product_id" HeaderText="product_id"
InsertVisible="False" ReadOnly="True" SortExpression="product_id" />
<asp:BoundField DataField="product_name" HeaderText="product_name"
SortExpression="product_name" />
<asp:BoundField DataField="product_desc" HeaderText="product_desc"
SortExpression="product_desc" />
<asp:BoundField DataField="product_cost" HeaderText="product_cost"
SortExpression="product_cost" />
<asp:BoundField DataField="product_amount" HeaderText="product_amount"
SortExpression="product_amount" />
<asp:BoundField DataField="category_id" HeaderText="category_id"
SortExpression="category_id" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Fandangles.mdb"
DeleteCommand="DELETE FROM [product] WHERE [product_id] = ?"
InsertCommand="INSERT INTO [product] ([product_name], [product_desc], [product_cost], [product_amount], [category_id]) VALUES (?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [product]"
UpdateCommand="UPDATE [product] SET [product_name] = ?, [product_desc] = ?, [product_cost] = ?, [product_amount] = ?, [category_id] = ? WHERE [product_id] = ?">
<DeleteParameters>
<asp:Parameter Name="product_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
<asp:Parameter Name="product_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="product_id" Type="Int32" />
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
</InsertParameters>
</asp:AccessDataSource>
</p>
</asp:Content>
DB: Access DB
product_id - Autonumber
product_name - Text
product_desc - Text
product_cost - Currency
product_amount - Number
category_id - Number - This is a lookup field
Any ideas?
Do you really need to pass your primary key "product_id" into the insert? If its an identity / PK its not needed.
Its not a param in your insert SQL yet it is a param in the datasoruces insert params.
Try this
<InsertParameters>
<asp:Parameter Name="product_id" Type="Int32" />
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
</InsertParameters>

Add aspnet_users UserName value into my own table

I would like to insert UserName from aspnet_users into my own table called Members. My sqldatasource looks like as follows. At the end in the InsertParameters I have added ProfileParameter but its not working. Just to get the values going, I have hardcoded 'testhere' value in the insertcommand. My SQLDatasource looks as follows.
Please help !!!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
OnInserted= "on_inserted"
OnUpdated = "on_updated"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
DeleteCommand="DELETE FROM [member] WHERE [id] = #id"
InsertCommand="INSERT INTO [member] ([firstname], [lastname],[username]) VALUES (#firstname, #lastname,'testhere');
set #id = ##identity"
SelectCommand="SELECT * FROM [member] WHERE ([id] = #id)"
UpdateCommand="UPDATE [member] SET [firstname] = #firstname, [lastname] = #lastname WHERE [id] = #id"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type=Int32 Direction=Output />
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:ProfileParameter Name="username" Type=String />
</InsertParameters>
</asp:SqlDataSource>
Okay.. as a simple one:
Try changing the InsertParameter (username) to be an asp:Parameter.
Add a asp:HiddenField control with ID of username
onpageload, set the value of the username hiddenfield to be Page.User.Identity.Name
Magic!

Resources