Insert proper phone format - c#

I have a textbox which stores phone number.
<asp:TextBox runat="server" ID="phone" MaxLength="10" Columns="10" Width="90px" />
<asp:MaskedEditExtender ID="phone_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" TargetControlID="phone" Mask="(999)-999-9999" ClearMaskOnLostFocus="False">
</asp:MaskedEditExtender>
The question is when I insert phone number 9999999999, the number in the database becomes
(999)-9999.
What I want is 9999999999.
The other code is:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:MembershipDB %>"
InsertCommand="CreateAdditionalAccountInfo" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:ControlParameter Name="FirstName" Type="String" ControlID="FirstName" PropertyName="Text" />
<asp:ControlParameter Name="LastName" Type="String" ControlID="LastName" PropertyName="Text" />
<asp:ControlParameter Name="Degree" Type="String" ControlID="Degree" PropertyName="Text" />
<asp:ControlParameter Name="Organization" Type="String" ControlID="Organization"
PropertyName="Text" />
<asp:ControlParameter Name="Phone" Type="String" ControlID="Phone" PropertyName="Text" />
<asp:ControlParameter Name="Ext" Type="String" ControlID="Ext" PropertyName="Text" />
<asp:ControlParameter Name="last4SSN" Type="String" ControlID="TextSSN" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
The stored Procedure query is
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId, #FirstName, #LastName, #Degree, #Organization, #Phone, #Ext
Thanks.

Change the proc to this:
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId
, #FirstName
, #LastName
, #Degree
, #Organization
, REPLACE(REPLACE( REPLACE(#Phone,'(',''),')','') ,'-','') , #Ext )
The reason I suggest this approach is because you are using a SQLDatasource object and I don't think it's possible to instruct the insert parameter -at least not declaratively from the markup- to ignore the mask. Your best bet is probably just to modify the proc.
UPDATE - programmatic approach:
Add a handler for OnInserting to the SqlDataSource:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" OnInserting="On_Inserting"
Handle the event on code and do the replacement there:
protected void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e) {
e.Command.Parameters["#Phone"].Value=phone.Text.Replace("..."); // etc
}
Note: not tested but it's the idea.

Related

ASP.NET label in HTML accessDataSource

I need to replace the ? with lblDates.Text,how is that possible?? lblDates is a label to which I saved a date(string) from calendar,so I need this so I can fill the gridview
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_data/Baza1.accdb"
SelectCommand="SELECT [Spol], [Prezime], [Vrijeme], [Stol] FROM [rezervacije] WHERE ([Datum] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="lblDates" Name="Datum" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:AccessDataSource
Since the ControlParameter that retrieves lblDates.Text has Name="Datum", replace ? in SelectCommand with #Datum:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_data/Baza1.accdb"
SelectCommand="SELECT [Spol], [Prezime], [Vrijeme], [Stol] FROM [rezervacije] WHERE ([Datum] = #Datum)">
<SelectParameters>
<asp:ControlParameter ControlID="lblDates" Name="Datum" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:AccessDataSource>
For more information, see Using Parameters with Data Source Controls.

SqlDataSource InsertCommand Missing Required Parameters

I keep getting an OleDbException :"No value given for one or more required parameters" - Not very specific.
I have had no luck finding a solution to my problem online. I have tried many different things like not declaring the Parameters in the form and creating them at run time in the code behind.
Here is my ugly code:
<asp:sqldatasource ID="datasource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ID], [Test Data] AS Test_Data, [More Data] AS More_Data FROM [Main]"
UpdateCommand="UPDATE [Main] SET [Test Data]=#TestData, [More Data]=#MoreData WHERE [ID]=#ID"
InsertCommand="INSERT INTO [Main] ([ID], [Test Data], [More Data]) VALUES (#InsertID, #InsertTestData, #InsertMoreData)"
DeleteCommand="" >
<UpdateParameters>
<asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
<asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
<asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
<asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
<asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
<asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:sqldatasource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="true" DataSourceID="datasource"></asp:GridView>
<br />
<asp:DropDownList ID="insertIDD" DataSourceID="datasource" DataTextField="ID" runat="server" Font-Bold="False"></asp:DropDownList>
<asp:TextBox ID="insertTest" runat="server"></asp:TextBox>
<asp:TextBox ID="insertMore" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Insert" />
<br />
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="datasource" DataTextField="ID" OnTextChanged="populateDrop" AutoPostBack="true"></asp:DropDownList>
<asp:TextBox ID="updateTest" runat="server"></asp:TextBox>
<asp:TextBox ID="updateMore" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Update" OnClick="Update" />
<br />
<asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Delete" />**
And the C# Code Behind:
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
DataView dv = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
dv = (DataView)datasource.Select(DataSourceSelectArguments.Empty);
updateTest.Text = dv[0]["Test_Data"].ToString();
updateMore.Text = dv[0]["More_Data"].ToString();
}
protected void Insert(object sender, EventArgs e)
{
datasource.Insert();
}
protected void Update(object sender, EventArgs e)
{
datasource.Update();
updateTest.Text = "";
updateMore.Text = "";
}
SELECT and UPDATE work without any problem. No matter what I try I cannot get the INSERT INTO command to be happy. I am sure it is something simple that I am missing, but any help would be greatly appreciated, thanks!
this is for Update:
<UpdateParameters>
<asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
<asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
<asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
</UpdateParameters>
and this is for Insert
<InsertParameters>
<asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
<asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
<asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
</InsertParameters>
At the end:
<asp:sqldatasource ID="datasource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ID], [Test Data] AS Test_Data, [More Data] AS More_Data FROM [Main]"
UpdateCommand="UPDATE [Main] SET [Test Data]=#TestData, [More Data]=#MoreData WHERE [ID]=#ID"
InsertCommand="INSERT INTO [Main] ([ID], [Test Data], [More Data]) VALUES (#InsertID, #InsertTestData, #InsertMoreData)"
DeleteCommand="" >
<UpdateParameters>
<asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
<asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
<asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
</UpdateParameters>
<InsertParameters>
<asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
<asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
<asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
</InsertParameters>
</asp:sqldatasource>

Inserting into SQL Server database ASP.net

What I'm trying to do is to insert a username, and their monthly hour limit to my SQL Server database. I've used the automatically generated statements for updating and deleting. I just need to add in new users now. The code below, should work as far as I know, but it doesn't. I think it's the way I've written it.
The part in comments is what the Userdata.aspx file automatically generated, so I'm trying to convert it to use my 2 text boxes.
Thanks a lot.
protected void Button1_Click1(object sender, EventArgs e)
{
string sql = "INSERT INTO [UserData]([UserName], [MonthlyHourLimit]) VALUES ("+ TextBox1.Text + "," + TextBox2.Text + ")";
//INSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit)"
SqlDataSource1.InsertCommand = sql;
GridView1.DataBind();
}
You need to configure your data source to use parameters.
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= #UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Direction="Input" Type="String" />
<asp:Parameter Name="MonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
UPDATE:I've forgot to mention, you would like to use ControlParameter and not simple Parameter. Take a look at following snippet:
<asp:СontrolParameter Name="UserName" ControlId="ddlUserNames" PropertyName="SelectedValue"/>
...
<asp:DropdownList
ID="ddlUserNames"
runat="server"
Autopostback="True">
<asp:Listitem Selected="True">Users</asp:Listitem>
<asp:Listitem Value="Peter">Peter</asp:Listitem>
<asp:Listitem Value="Jessica">Jessica</asp:Listitem>
</asp:Dropdownlist>
Take a look at corresponding MSDN page describing usage of SqlDataSource in details.
UPDATED 2: complete example in order to avoid confusion
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= #UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (#UserName, #MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="UserName" ControlId="txtUserName" Direction="Input" Type="String" />
<asp:ControlParameter Name="MonthlyHourLimit" ControlId="txtMonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
<asp:TextBox runat="server" ID="txtUserName" />
<asp:TextBox runat="server" ID="txtMonthlyHourLimit" />
Datasource.InsertCommand is a property.
Datasource.Insert() is a method.
You should also use parameters.
datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text

DetailsView update

I have a GridView that shows all the registed users and some of their information and when you choose the "select" link off the side, it shows a DetailsView with all the details related to that user. The select works fine and updates the details view for the selected user, and when I try to edit the DetailsView, it will update the database and fill in the information updated for ALL the users when changing the fields (eg. if I change the customers first name, everyone registered will get that name). Here's my aspx code for my SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [UserProfile] ORDER BY [Email], [LastName], [Company]"
UpdateCommand="UPDATE [UserProfile] SET [LastName] = #LastName, [PartsList] = #PartsList, [UserName] = #UserName, [Question] = #Question,
[Answer] = #Answer, [Role] = #Role
WHERE [FirstName] = FirstName AND [Company] = Company">
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Company" Type="String" />
<asp:Parameter Name="PartsList" Type="String" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="Answer" Type="String" />
<asp:Parameter Name="Role" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
You forgot #original_ in your Where parameters
Change UpdateCommand to:
UPDATE [UserProfile] SET [LastName] = #LastName, [PartsList] = #PartsList, [UserName] = #UserName, [Question] = #Question,
[Answer] = #Answer, [Role] = #Role
WHERE [FirstName] = #original_FirstName AND [Company] = #original_Company

ASP.NET : SqlDataSource insert - identity parameter

I have an asp.net application in which I am inserting into an sql table using a GridView linked to an SqlDataSource. The problem is that I do not know the 'Identity' parameter to use on insertion. The 'Identity' column should be automatically incrementing, however upon inserting, I get an exception that the Identity param must not be null. How do I either craft the insert statement so that this works or how do I get the next correct identity # to specify? Thanks.
Here is my SqlDataSource code:
<asp:SqlDataSource ID="VehiclesSqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:RamRideOpsConnectionString %>"
SelectCommand="SELECT * FROM [Vehicles]"
ConflictDetection="CompareAllValues"
InsertCommand="INSERT INTO [Vehicles] ([Identity], [CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#Identity, #CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)"
OnInserting="VehiclesSqlDS_Insert"
OldValuesParameterFormatString="original_{0}">
<InsertParameters>
<asp:Parameter Name="Identity" Type="Int32 "/>
<asp:Parameter Name="CarNum" Type="Int32" DefaultValue="-1"/>
<asp:Parameter Name="MaxPassengers" Type="Int32" DefaultValue="3" />
<asp:Parameter Name="Status" Type="String" DefaultValue="Ready" />
<asp:Parameter Name="CurrPassengers" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="StartAdd" Type="String" />
<asp:Parameter Name="EndAdd" Type="String" />
<asp:Parameter DbType="Time" Name="AvgRideTime" />
<asp:Parameter Name="numRides" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:SqlDataSource>
you need to set parameter direction to OUTPut, to get the identity value. from MSDN
and handle the onInserted event to get, the identity value
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
string sID = e.Command.Parameters["#Identity"].Value.ToString();
//Display new ID
}
you can try like this.
<asp:sqlDataSource ID="Datasource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = #EmpID"
InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (#LastName, #FirstName);
SELECT #EmpID = SCOPE_IDENTITY()"
UpdateCommand="UPDATE Employees SET LastName=#LastName, FirstName=#FirstName
WHERE EmployeeID=#EmployeeID"
DeleteCommand="DELETE Employees WHERE EmployeeID=#EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:sqlDataSource>
You should omit the Identity column from the insert statement. Only include the data columns. The Sql Server will create the auto-increment then.
INSERT INTO [Vehicles] ([CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)
Ever Present, that is assuming that Identity is an auto-incremeneted column. It could just be a regular column.
You need to somehow retrieve the latest Identity first before trying to insert.
Alternatively you could write a trigger on the database that automatically updates the Identity column when a new item is added.
These snippets may help:
<InsertParameters>
<asp:Parameter Name="SalesRepID" Type="Int32" />
...more params
<asp:Parameter Name="QuoteID" Type="Int32" Direction="Output" />
</InsertParameters>
Note the second command added
InsertCommand="INSERT INTO [MyTable] (fields); SET #QuoteID = Scope_Identity();"

Categories

Resources