7 Replies - 6457 Views - Last Post: 31 July 2011 - 07:21 AM Rate Topic: -----

#1 visu862  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-March 11

Save button

Posted 26 July 2011 - 08:33 PM

hi i am doing final year project car rental system. i am using asp.net and c# for develop my project. i create code for save button but its still cant run in the browser and cant save into the database table. After run in the page its show "A page can have only one server-side Form tag."
hope will replay my post.thank you.

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;




public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString;
    }

    private void ExecuteInsert(string customer_id, string name, string gender, string calculate, string address, string nationality, string email, string mobile, string license, string rentdate, string returndate, string total)
    {


        SqlConnection conn = new SqlConnection (GetConnectionString());

        string sql = "INSERT INTO rental (customer_id, name, gender, address, nationality, email, mobile, license, rentdate, returndate, calculate, total) VALUES " + " (customer_id, name, gender, address, nationality, email, mobile, license, rentdate, returndate, calculate, total)";

        try
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[12];


            param[0] = new SqlParameter("@customer_id",SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@name", SqlDbType.VarChar, 50);
            param[2] = new SqlParameter("@gender", SqlDbType.VarChar, 50);
            param[3] = new SqlParameter("@address", SqlDbType.VarChar, 50);
            param[4] = new SqlParameter("@nationality", SqlDbType.VarChar, 50);
            param[5] = new SqlParameter("@email", SqlDbType.VarChar, 50);
            param[6] = new SqlParameter("@mobile", SqlDbType.VarChar, 50);
            param[7] = new SqlParameter("@licence", SqlDbType.VarChar, 50);
            param[8] = new SqlParameter("@rentdate", SqlDbType.VarChar, 50);
            param[9] = new SqlParameter("@returndate", SqlDbType.VarChar, 50);
            param[10] = new SqlParameter("@calculate", SqlDbType.VarChar, 50);
            param[11] = new SqlParameter("@total", SqlDbType.VarChar, 50);



            param[0].Value = customer_id;
            param[1].Value = name;
            param[2].Value = gender;
            param[3].Value = address;
            param[4].Value = nationality;
            param[5].Value = email;
            param[6].Value = mobile;
            param[7].Value = license;
            param[8].Value = rentdate;
            param[9].Value = returndate;
            param[10].Value = calculate;
            param[11].Value = total;

            

            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        ExecuteInsert(txtcustomer_id.Text, txtname.Text, txtgender.Text, txtaddress.Text, txtnationality.Text, txtemail.Text, txtmobile.Text, txtlicence.Text, txtrentdate.Text, txtreturndate.Text, txtcalculate.Text, txttotal.Text);

        Response.Write("Record was succesfully added!");
        
    }
    protected void txtcalculate_TextChanged(object sender, EventArgs e)
    {

    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Save button

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,753
  • Joined: 08-June 10

Re: Save button

Posted 26 July 2011 - 09:09 PM

The error is self-explanatory. You can only have one form tag (with runat=server) on a single page. That's not an issue with the codebehind, it's an issue with the markup.

Also, your SQL statement isn't going to work. Notice how your parameters have the @ symbol, but the parameters in the SQL statement do not. They need to. Also, look into using cmd.Parameters.AddWithValue to make it much less clunky to add your parameters. There's no need to make an array of them that way.'

Lastly, moving to ASP.NET.
Was This Post Helpful? 0
  • +
  • -

#3 visu862  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-March 11

Re: Save button

Posted 27 July 2011 - 06:01 AM

hi. thank you for your post. but can you explain more deeply witch can make me understand. thank you
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Save button

Posted 27 July 2011 - 10:26 AM

There was more than one problem covered above, which one do you need to have discussed and what part of it are you not understanding?

This post has been edited by Nakor: 27 July 2011 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Save button

Posted 28 July 2011 - 05:01 AM

View Postvisu862, on 27 July 2011 - 09:01 AM, said:

hi. thank you for your post. but can you explain more deeply witch can make me understand. thank you


Post the markup code.
Was This Post Helpful? 0
  • +
  • -

#6 visu862  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-March 11

Re: Save button

Posted 30 July 2011 - 05:17 AM

i try my best to identify the problem and make some changes in my code. can anyone explain where and what is my wrong in my code but still have this error "A page can have only one server-side Form tag".




 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;




public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString;
    }

    private void ExecuteInsert(string customer_id, string name, string gender, string calculate, string address, string nationality, string email, string mobile, string license, string rentdate, string returndate, string total)
    {


        SqlConnection conn = new SqlConnection (GetConnectionString());

        string sql = "INSERT INTO rental (customer_id, name, gender, address, nationality, email, mobile, license, rentdate, returndate, calculate, total) VALUES " + " (customer_id, name, gender, address, nationality, email, mobile, license, rentdate, returndate, calculate, total)";

        try
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[12];


            param[0] = new SqlParameter("customer_id",SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("name", SqlDbType.VarChar, 50);
            param[2] = new SqlParameter("gender", SqlDbType.VarChar, 50);
            param[3] = new SqlParameter("address", SqlDbType.VarChar, 50);
            param[4] = new SqlParameter("nationality", SqlDbType.VarChar, 50);
            param[5] = new SqlParameter("email", SqlDbType.VarChar, 50);
            param[6] = new SqlParameter("mobile", SqlDbType.VarChar, 50);
            param[7] = new SqlParameter("licence", SqlDbType.VarChar, 50);
            param[8] = new SqlParameter("rentdate", SqlDbType.VarChar, 50);
            param[9] = new SqlParameter("returndate", SqlDbType.VarChar, 50);
            param[10] = new SqlParameter("calculate", SqlDbType.VarChar, 50);
            param[11] = new SqlParameter("total", SqlDbType.VarChar, 50);



            param[0].Value = customer_id;
            param[1].Value = name;
            param[2].Value = gender;
            param[3].Value = address;
            param[4].Value = nationality;
            param[5].Value = email;
            param[6].Value = mobile;
            param[7].Value = license;
            param[8].Value = rentdate;
            param[9].Value = returndate;
            param[10].Value = calculate;
            param[11].Value = total;

            

            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        ExecuteInsert(txtcustomer_id.Text, txtname.Text, txtgender.Text, txtaddress.Text, txtnationality.Text, txtemail.Text, txtmobile.Text, txtlicence.Text, txtrentdate.Text, txtreturndate.Text, txtcalculate.Text, txttotal.Text);

        Response.Write("Record was succesfully added!");
        
    }
    protected void txtcalculate_TextChanged(object sender, EventArgs e)
    {

    }
}



Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Save button

Posted 30 July 2011 - 07:55 AM

it's not referring to your code, it's referring to the .aspx files. You have more than one <form runat=server> element in your aspx files, not your cs files. If you're using a master page then there is already a form tag in the master page and one would not be needed in the individual pages. If you're using more than one <form> element in the aspx page, don't.

Also we would need to see the markup from the .aspx file, not the .cs file, the code you posted has nothing to do with the problem.

edit: also, you missed some of what Curtis Rutland said concerning the @ sign in your sql statement. The variables in the statement need to have the @ sign on them.

This post has been edited by Nakor: 30 July 2011 - 07:58 AM

Was This Post Helpful? 1
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Save button

Posted 31 July 2011 - 07:21 AM

I am sorry, but if you don't know the difference between markup and code-behind, then you have even bigger issues.

Your problem is in the markup, NOT the code-behind.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1