Wednesday, March 28, 2012

InsertCommand, add strings...

I am submitting a telephone number into a table. I have 3 boxes for the telephone number. Telephone1,Telephone2,Telephone3. I need to insert the values of the 3 text boxes into a column called phone in my table.

so like

InsertCommand="INSERT INTO customer_mod (phone) Values (@.Telephone_1)

<asp:formparameter name="Telephone_1" formfield="Telephone1+Telephone2+Telephone3" />

I don't think that is gonna work, so can you please help me make that code work?

I think you'll have to write some code for this. Write below code in the submit button click event.

string Telephone =string.Empty;
Telephone = Telephone1.Text +"-" + Telephone2.Text +"-" + Telephone3.Text;
SqlDataSource1.InsertParameters.Add("@.Telephone", Telephone);
SqlDataSource1.Insert();

Hope this will help.

|||

That works, but then it does not submit any of my other data. I have data being inserted into the database using...

InsertCommand="INSERT INTO customer_mod (CsrAgent,FirstName,LastName,Address1,City1,State1,Zipcode1,

InstallAddress,InstallCity,InstallState,InstallZip,CrossStreets,InstallDate,InstallTimes,InstallTimesOther)

When using this on the button...

string phone =string.Empty;

phone = Telephone1.Text + Telephone2.Text + Telephone3.Text;

SqlDataSource1.InsertParameters.Add("@.phone", phone);

SqlDataSource1.InsertCommand ="INSERT INTO [customer_mod](phone) values ('" + phone +"')";

SqlDataSource1.Insert();

Only the data is shown. So basically it is only accepting one insert. Can I not use insert from my aspx page and cs page?

|||

This was just to demonstrate you how you can merge the telephone numbers. In order to pass all other values, you just need to pass the value for each column as a parameter. So your insert query would be like:

INSERT INTO customer_mod ( CsrAgent , FirstName , LastName , Address1 , City1 , State1 , Zipcode1 , InstallAddress , InstallCity , InstallState , InstallZip , CrossStreets , InstallDate , InstallTimes , InstallTimesOther )values ( @.CsrAgent , @.FirstName , @.LastName , @.Address1 , @.City1 , @.State1 , @.Zipcode1 , @.InstallAddress , @.InstallCity , @.InstallState , @.InstallZip , @.CrossStreets , @.InstallDate , @.InstallTimes , @.InstallTimesOther )

and you'll have to add the value for each of the above parameters just like you did for phone number. Get the value from the text box or any other control on your form and pass it to the value for that parameter.

No comments:

Post a Comment