You must Sign In to post a response.
  • Category: ASP.NET

    Insert statement in asp.net

    Hi,
    How to inset data in database
    my example on below.
    class.cs
    ---------
    public void class()
    {
    string fname {get;set;};
    string lname{get;set;};
    }

    i have referred this class in my aspx.cs page

    class objclass = new class();

    now how can i use this class in my insert statement

    string insert="insert into tablename(val1,val2) values ('"+textbox1.text+"','"+textbox2.text+"')";
    sqlcommend cmd= new sqlcomment(insert,connectionstring);
    cmd.executenonquery();

    in this place how can i use class for this insert statement
  • #767992
    Hi,

    We can make a simple methods to insert the data into database using asp.net.
    Kindly check the following example are take the one which feel like easy.


    string connectionString = "Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=AddressBook;Server=Bilal-PC";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    SqlCommand cmd = new SqlCommand();

    cmd.CommandText = "INSERT INTO Data (Name,PhoneNo,Address) VALUES (" + txtName.Text + "," + txtPhone.Text + "," + txtAddress.Text + ");";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;

    connection.Open();
    cmd.ExecuteNonQuery();
    }



    You can also do with parameterized tags like this,


    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    SqlCommand cmd = new SqlCommand("INSERT INTO Data (Name, PhoneNo, Address) VALUES (@Name, @PhoneNo, @Address)");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    cmd.Parameters.AddWithValue("@Name", txtName.Text);
    cmd.Parameters.AddWithValue("@PhoneNo", txtPhone.Text);
    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
    connection.Open();
    cmd.ExecuteNonQuery();
    }


    or by general way,


    SqlCommand cmd = new SqlCommand("insert into Data (Name,PhoneNo,Address) values(@parameter1,@parameter2,@parameter3)",con);
    cmd.Parameters.AddWithValue("@parameter1", (textBox1.Text));
    cmd.Parameters.AddWithValue("@parameter2", textBox2.Text);
    cmd.Parameters.AddWithValue("@parameter3", (textBox3.Text));
    cmd.ExecuteNonQuery();


    Thanks,
    Mani

  • #767994
    Hi all thanks for your response.. but how can i use that class variables inside of method.. pls

  • #767997
    Hey G Rajasekaran,

    here i sent you some solution you,

    In aspx:

    <asp:LiteralControl runat="server" id="literalControl1"></LiteralControl>

    In Page_Load / code behind:

    literalControl1.Text = "<table><tr>... whatever";

    You can do it with <%= variable %> as well, but it's nicer to do it in code behind.

    For a reference on <%-tags, go to http://samples.gotdotnet.com/quickstart/aspplus/doc/webformssyntaxref.aspx



    To access properties defined in code-behind inside your HTML part, you would need to define your proeprties or variables as public, and then use them as follows:

    <%= this.MyVariable %>




    Even though this post is old, it sends out a not-so-good idea that one should use Label/Literal to build the HTML using the variables to achieve this objective. This is not always good because we are loading the server with the job of creating the HTML for you, which could've easily done using the raw HTML itself.

    if the requirement is simple, it can be preferred to be done by just using the public variable name <%= this.MyVariable %> as Haidar mentioned above. You can also use any of the approaches mentioned at the below URL to fetch the variable value and use it.

    http://websummaries.blogspot.com/2010/12/pass-and-use-variables-in-aspnet-server.html

    If the requirement is complex enough that none of these options works out, consider HTML generation using server code at which case Literal is the ideal way to go!



    Hope this helps,
    Regards

    Thanks,
    Vaibhav Shah

  • #768019
    Hi, here is the answer of your query:
    //1.Assuming you have aspx with two text boxes (txtFirstName & txtLastName)
    //2.Now, Set properties of your class like below under button click event and pass the object directly to InsertRecords method:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    YourClassName classObject = new YourClassName()
    {
    fname = txtFirstName.text,
    lname = txtLastName.text
    };

    //Invoke the common insert method "InsertRecords" to insert records as shown below

    InsertRecords(classObject);//this wil insert the records into your table
    }



    public void InsertRecords(YourClassName classObject)
    {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    SqlCommand cmd = new SqlCommand("INSERT INTO YourTable (fname, lname) VALUES (@fname, @lname)");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    cmd.Parameters.AddWithValue("@fname", classObject.fname);
    cmd.Parameters.AddWithValue("@lname", classObject.lname);
    connection.Open();
    cmd.ExecuteNonQuery();
    }
    }
    contact for any other asp.net related query.

  • #768027
    Hai G Rajasekaran,
    To use the Class variables in your method, you need to first create the object of the class and then initialize with the values and then you can use them in the method as below:

    public void class()
    {
    string fname {get;set;};
    string lname{get;set;};
    }

    In the method:

    class objclass = new class();
    objclass.fname = textbox1.text;
    objclass.lname = textbox2.text;
    string insert="insert into tablename(val1,val2) values ('"+objclass.fname+"','"+objclass.lname+"')";
    sqlcommend cmd= new sqlcomment(insert,connectionstring);
    cmd.executenonquery();

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #768028
    Hi,


    Refer the below sample for your requirement, like inserting when you click on login ex:

    UI:
    Login Page
    public partial class login : System.Web.UI.Page
    {
    businessLogic obj = new businessLogic();

    protected void button_Click(object sender, EventArgs e)
    {
    obj.fname= fname.Text;
    obj.lname= lname.Text;
    obj.loginbll();
    Response.Redirect("HOme.aspx");
    }
    }

    BLL:

    using databaselogic;
    public class businessLogic
    {
    public businessLogic ()
    {
    databaselogic obj_Bl = new databaselogic();
    }

    public string _fname , _lname;

    public string fname
    {
    get { return _fname; }
    set { _fname = value; }
    }

    public string lname
    {
    get { return _lname; }
    set { _lname = value; }
    }

    public void loginbll()
    {
    obj_Bl.login(fname,lname);
    }

    DAL:

    namespace databaselogic
    {

    public class databaselogic
    {
    public databaselogic()
    {
    }
    public void login(string fname, string lname)
    {
    //code related to Queries goes on here
    string insert="insert into tablename(val1,val2) values (fname,lname)";
    sqlcommend cmd= new sqlcomment(insert,connectionstring);
    cmd.executenonquery();
    }
    }
    }


    Hope this will give you an idea

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring


  • Sign In to post your comments