i have ran into some problems with my insert function.
i am trying to make my the string listing_ID an auto increment int with no input needed, however when coding for the button submit i included all the inputs for all the other values but not listing_ID , this gave me the error below. below are all the images and these are the codes for the insert function.
public class Carlisting
{ private string _listID = ""; private string _car_model = ""; private string _brand_name = ""; private string _car_description = ""; private string _car_condition = ""; private string _price = ""; private string _inspection_date = ""; string _connStr = ConfigurationManager.ConnectionStrings["roadbnb.mdf"].ConnectionString; public Carlisting(string listID, string car_model, string brand_name, string car_description, string car_condition, string price, string inspection_date) { _listID = listID; _car_model = car_model; _brand_name = brand_name; _car_description = car_description; _car_condition = car_condition; _price = price; _inspection_date = inspection_date; } public Carlisting() { } public string listing_ID { get { return _listID; } set { _listID = value; } } public string car_model { get { return _car_model; } set { _brand_name = value; } } public string brand_name { get { return _brand_name; } set { _brand_name = value; } } public string car_description { get { return _car_description; } set { _car_description = value; } } public string car_condition { get { return _car_condition; } set { _car_condition = value; } } public string price { get { return _price; } set { _price = value; } } public string inspection_date { get { return _inspection_date; } set { _inspection_date = value; } }
protected void btn_submit_Click(object sender, EventArgs e)
{ int result = 0; Carlisting car = new Carlisting(tb_model.Text, tb_brand.Text, tb_description.Text, dl_condition.Text, tb_price.Text, tb_date.Text); result = car.ListingInsert(); if (result > 0) { Response.Write("<script>alert('You have succesfully added listing , PLease wait for approval');</script>"); } else { Response.Write("<script>alert('Error : PLease contact helpdesk');</script>"); } }
public int ListingInsert()
{
int result = 0; string queryStr = "INSERT INTO Carlisting(car_model, brand_name, car_description, car_condition , price, inspection_date)" +"VALUES (@car_model, @brand_name, @car_description, @car_condition, @price, @inspection_date)"; SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); cmd.Parameters.AddWithValue("@car_model", this.car_model); cmd.Parameters.AddWithValue("@brand_Name", this.brand_name); cmd.Parameters.AddWithValue("@car_description", this.car_description); cmd.Parameters.AddWithValue("@car_condition", this.car_condition); cmd.Parameters.AddWithValue("@price", this.price); cmd.Parameters.AddWithValue("@inspection_date", this.inspection_date); conn.Open(); result += cmd.ExecuteNonQuery(); conn.Close(); return result; }
Does anyone know how should fix it in order to get the results i wan? Thank you in advance
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As per your screenshot, you are getting compilation error. To Fix it, create another constructor. Currently your code want to invoke constructor which does not take listId
as parameter.
public Carlisting(string listID, string car_model, string brand_name, string car_description, string car_condition, string price, string inspection_date) : this(car_model, brand_name, car_description, car_condition, price, inspection_date) { _listID = listID; } public Carlisting(string car_model, string brand_name, string car_description, string car_condition, string price, string inspection_date) { _car_model = car_model; _brand_name = brand_name; _car_description = car_description; _car_condition = car_condition; _price = price; _inspection_date = inspection_date; }
With above the 1st constructor invokes the another constructor with other required parameters. And for your code, the 2nd constructor will be invoked and you won’t have compilation error.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0