This is the front of my page:
<div >
<asp:Button style="background-color: #007aff; width: 100%; padding: 5px" Text="SAVE" OnClick="Insert_Click" ID="InsertID" runat="server"/>
</div>
This is the back with each datas needed to reach the db as parameters:
protected void Insert_Click(string StrImage, string StrMember, int intPersonalID, string StrFirstName, string StrLastName,
string StrSex, string StrEmail, string StrPhoneNumber, string StrAddress, string StrDateAdded, string StrEmergencyContact,
string StrEmergencyRelationship, string StrEmergencyPhone)
{
SqlCommand command;
SqlConnection connection;
connection = new SqlConnection(constStringConnection);
connection.Open();
command = new SqlCommand("Learner_Db_Edit", connection);
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("Image", StrImage);
command.Parameters.AddWithValue("Member", StrMember);
command.Parameters.AddWithValue("PersonalID", intPersonalID);
command.Parameters.AddWithValue("Firstname", StrFirstName);
command.Parameters.AddWithValue("Lastname", StrLastName);
command.Parameters.AddWithValue("Sex", StrSex);
command.Parameters.AddWithValue("Email", StrEmail);
command.Parameters.AddWithValue("PhoneNumber", StrPhoneNumber);
command.Parameters.AddWithValue("Address", StrAddress);
command.Parameters.AddWithValue("DateAdded", StrDateAdded);
command.Parameters.AddWithValue("EmergencyContact", StrEmergencyContact);
command.Parameters.AddWithValue("EmergencyRelationship", StrEmergencyRelationship);
command.Parameters.AddWithValue("EmergencyPhone", StrEmergencyPhone);
command.ExecuteNonQuery();
connection.Close();
}
and my database
ALTER PROCEDURE [dbo].[Learner_Db_Edit]
@id int,
@NewImage varchar(50),
@NewMember varchar(50),
@NewPersonalID varchar(50),
@NewFirstName varchar(50),
@NewLastName varchar(50),
@NewSex varchar(50),
@NewEmail varchar(50),
@NewPhoneNumber varchar(50),
@NewAddress varchar(50),
@NewDate varchar(50),
@NewEmergencyContact varchar(50),
@NewEmergencyRelationship varchar(50),
@NewEMergencyPhone varchar(50)
AS
UPDATE [dbo].[Members]
SET
Image = @NewImage,
Member = @NewMember,
PersonalID = @NewPersonalID,
Firstname = @NewFirstName,
Lastname = @NewLastName,
Sex = @NewSex,
Email = @NewEmail,
PhoneNumber = @NewPhoneNumber,
Address = @NewAddress,
DateAdded = @NewDate,
EmergencyContact = @NewEmergencyContact,
EmergencyRelationship = @NewEmergencyRelationship,
EmergencyPhone = @NewEMergencyPhone
WHERE ID = @id;
I thought maybe the issue was with the ID since it's not supposed to be altered from database, but it was not the case. Also tried with the date because it should not change but nope. Please help. I know the error is coming from the back-end, but i can't tell what I'm missing.
CodePudding user response:
when you double click on a button, or in markup type in
OnClick=
When you hit the "=" sign, then you get intel-sense to create this:
You MUST let visual studio create the code behind stub, and it MUST 100% match this ALWAYS:
So after a double click on the button (from the designer), or typing in above "onclick=" and choosing create new event?
You get a code stub like this:
protected void InsertID_Click(object sender, EventArgs e)
{
}
You can't just OUT of the blue CHANGE the parameters for the click event. They must and will ALWAYS 100% match the format as above.
So, if you muck around and change the format of the button click event then you will receive the error message you have.
just comment out your existing code stub, let VS re-create the code stub, and then you can re-paste back in your code inside of this new click event.
Now, with above in mind, then your click code would be something like this:
(but, WHERE is "ID" coming from? I don't see any place where you setup what ID you are working on???
so this:
protected void InsertID_Click(object sender, EventArgs e)
{
{
using (SqlConnection conn = new SqlConnection("your con string"))
{
using (SqlCommand command = new SqlCommand("Learner_Db_Edit", conn))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@NewImage",SqlDbType.NVarChar).Value = StrImage;
command.Parameters.Add("@NewMember", SqlDbType.NVarChar).Value = StrMember;
command.Parameters.Add("@NewPersonalID", SqlDbType.NVarChar).Value = intPersonalID;
command.Parameters.Add("@NewFirstname", SSqlDbType.NVarChar).Value = trFirstName;
command.Parameters.Add("@NewLastname", SqlDbType.NVarChar).Value = StrLastName;
command.Parameters.Add("@NewSex", SqlDbType.NVarChar).Value = StrSex;
command.Parameters.Add("@NewEmail", SqlDbType.NVarChar).Value = StrEmail;
command.Parameters.Add("@NewPhoneNumber", SqlDbType.NVarChar).Value = StrPhoneNumber;
command.Parameters.Add("@NewAddress", SqlDbType.NVarChar).Value = StrAddress;
command.Parameters.Add("@NewDateAdded", SqlDbType.NVarChar).Value = StrDateAdded;
command.Parameters.Add("@NewEmergencyContact", SqlDbType.NVarChar).Value = StrEmergencyContact;
command.Parameters.Add("@NewEmergencyRelationship", SqlDbType.NVarChar).Value = StrEmergencyRelationship;
command.Parameters.Add("@NewEmergencyPhone", SqlDbType.NVarChar).Value = StrEmergencyPhone;
command.ExecuteNonQuery();
}
}
}
}
HOWEVER, you don't show where all these string values are coming from????
Perhaps the button click is supposed to get pass values to your routine with all those values??
So, the above is "air code" and the general layout. But, you not shared (yet) where all these string values are to come from?
And it also not clear why in your database and stored procedure, all the columns are character type when some clearly look to be and should be of type int.