How to insert data into database?

i m new in asp m trying to insert data from textbox but i m failed:(

Page 1 of 1

5 Replies - 41103 Views - Last Post: 10 November 2008 - 12:24 PM Rate Topic: -----

#1 karachi_wala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-April 08

How to insert data into database?

Posted 10 November 2008 - 08:56 AM

SqlCommand oCmd = new SqlCommand();

        oCmd.CommandText = /*"Select O.OrderID, OrderDate, C.CompanyName as CustomerName, O.ShipAddress,FirstName as EmployeeName " +
                             "From Orders O, Customers C, Employees E " +
                             "where o.orderID = " + txtOrdertoFind.Text +
                             " and O.CustomerID = C.CustomerID and O.EmployeeID = E.EmployeeID ";*/
                             "Insert Into region(regionid)Values "+

                             (TextBox1.Text) + "east ";
        SqlCommand oCmd = new SqlCommand();
        oCmd1.CommandTex ="select regionid,regiondiscription from region";

           oCmd.Connection = oConn;
        SqlDataReader oReader = oCmd.ExecuteReader();
        oReader.Read();
        txtOrderNo.Text = oReader["regionID"].ToString();
        txtEmployee1.Text = oReader["regiondiscription"].ToString();



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to insert data into database?

#2 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: How to insert data into database?

Posted 10 November 2008 - 09:04 AM

Hi karachi_wala,

what's the error that you're getting.

Also, when you post code, put it in [ code ] [/ code ] tags, without the spaces, and your code will be nicely formatted in a code box like this:

SqlCommand oCmd = new SqlCommand();

oCmd.CommandText = /*"Select O.OrderID, OrderDate, C.CompanyName as CustomerName, O.ShipAddress,FirstName as EmployeeName " +
"From Orders O, Customers C, Employees E " +
"where o.orderID = " + txtOrdertoFind.Text +
" and O.CustomerID = C.CustomerID and O.EmployeeID = E.EmployeeID ";*/
"Insert Into region(regionid)Values "+

(TextBox1.Text) + "east ";
SqlCommand oCmd = new SqlCommand();
oCmd1.CommandTex ="select regionid,regiondiscription from region";

oCmd.Connection = oConn;
SqlDataReader oReader = oCmd.ExecuteReader();
oReader.Read();
txtOrderNo.Text = oReader["regionID"].ToString();
txtEmployee1.Text = oReader["regiondiscription"].ToString();



it makes it so that we can easily read the code :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to insert data into database?

Posted 10 November 2008 - 09:38 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#4 karachi_wala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-April 08

Re: How to insert data into database?

Posted 10 November 2008 - 10:14 AM

[quote name='karachi_wala' date='10 Nov, 2008 - 07:56 AM' post='460626']
SqlCommand oCmd = new SqlCommand();

        oCmd.CommandText = /*"Select O.OrderID, OrderDate, C.CompanyName as CustomerName, O.ShipAddress,FirstName as EmployeeName " +
                             "From Orders O, Customers C, Employees E " +
                             "where o.orderID = " + txtOrdertoFind.Text +
                             " and O.CustomerID = C.CustomerID and O.EmployeeID = E.EmployeeID ";*/
                             "Insert Into region(regionid)Values "+

                             (TextBox1.Text) + "east ";
        SqlCommand oCmd = new SqlCommand();
        oCmd1.CommandTex ="select regionid,regiondiscription from region";

           oCmd.Connection = oConn;
        SqlDataReader oReader = oCmd.ExecuteReader();
        oReader.Read();
        txtOrderNo.Text = oReader["regionID"].ToString();
        txtEmployee1.Text = oReader["regiondiscription"].ToString();



thnx i will follow the instruction next time
well i m new in asp.net plz give me a simle example how to insert data inte database from asp(front end)
here i m using NORTHWIND i hope u will help me thnx:)
Was This Post Helpful? 0
  • +
  • -

#5 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: How to insert data into database?

Posted 10 November 2008 - 11:38 AM

Ok, quick and dirty insert into db

Dim Conn As SqlConnection
Conn = New SqlConnection("[replace with connection string]")

Dim Cmd As SqlCommand
Dim SQL As String = ""

'Build your insert statement
SQL = "INSERT INTO [tbl] " & _
		  "([define columns you'll be inserting into]) " & _
		  "VALUES " & _
		  "([the values you'll be inserting into the above mentioned columns])"


' Initialize the SqlCommand with the new SQL string.
Cmd = New SqlCommand(SQL, Conn)

'attempt to execute the command
Try
  Cmd.ExecuteNonQuery()
Catch Ex As Exception
  'deal with exception
Finally
  'close the connection
  Cmd.Connection.Close()
End Try



Obviously, for values you can put items that are coming from a form
Was This Post Helpful? 0
  • +
  • -

#6 karachi_wala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-April 08

Re: How to insert data into database?

Posted 10 November 2008 - 12:24 PM

thnx fyrestorm 4 ur quick reply.
but i m still new in asp :) so wait 4 my next quistion :)

thnx again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1