Skip to content

Oracle Db Cresting table using C# query

Rajesh B

Asked Jul 31, 2013, 10:16 PM EDT10 answers
Share
LinkedInX
i have 4 textboxes and 3 buttons in a webpage, in create button onclick event it should create a table in that emp id(number not null), name varchar, joindate (date) should be there, then in the button click of insert it should insert the data which is entered in 3 textboxes, and there is one more button search is there when we provide emp id in 4 th textbox it should display the regarding emp id details.

aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ora con.aspx.cs" Inherits="ora_con" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



   
 




   
   

     
              
       
              OnSelectedIndexChanged="ddlcons" AutoPostBack="true">
   
   
   

   

  
           


  
           
           
               
               
               
           
           
               
               
               
           
           
               
               
               
           
       

       
               

       
               

                   
                 

   
               

                   
      
               

                                             style="height: 26px" />
      
               

                   
               

                     

                     
   

       

  
 
   


.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.OracleClient;

public partial class ora_con : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void btnora_Click(object sender, EventArgs e)
    {

        //try
        //{

            //use your own userid,password and datasorce
            string Constring;
            Constring = WebConfigurationManager.ConnectionStrings["QuickStarts Instance"].ConnectionString;
            OracleConnection con = new OracleConnection(Constring);
            con.Open();
            OracleCommand cmd = con.CreateCommand();
            cmd.CommandText = "CREATE TABLE con(id varchar2(20),name varchar2(50), 'DATE (DATE)')"; // i dont know hw to create date datatype here
            cmd.ExecuteNonQuery();
            Response.Write("");
        //}

        //catch (Exception)
        //{
        //    Response.Write("");
        //}
    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        {
            string Constring, query;
            Constring = WebConfigurationManager.ConnectionStrings["QuickStarts Instance"].ConnectionString;
            OracleConnection con = new OracleConnection(Constring);
            con.Open();
            query = "SELECT name,id FROM con where id = '" + TextBox3.Text.Trim() + "'";
            OracleCommand command = new OracleCommand(query, con);
            OracleDataReader reader = command.ExecuteReader();
            reader.Read();
            TextBox2.Text = reader[0].ToString();
            TextBox1.Text = reader[1].ToString();
            reader.Close();
            con.Close();
        }
    }



    protected void ddlcons(object sender, EventArgs e)
    {
        if (ddlcon.SelectedValue.ToString() == "1")
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
          //  TextBox3.Text = "";
        }
        else
        {
            Console.WriteLine("No Changes");
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        {
          
            string Constring;
            Constring = WebConfigurationManager.ConnectionStrings["QuickStarts Instance"].ConnectionString;
            OracleConnection con = new OracleConnection(Constring);
            con.Open();
            string str = "insert into con  (id) values ('" + TextBox1.Text + "')";//,'" + TextBox2.Text + "')";  // i dont know how to insert date value
              OracleCommand cmd = new OracleCommand(str, con);
            cmd.ExecuteNonQuery();
              con.Close();
            Response.Write("Inserted");
           

        }
    }
}

9 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I cant create the table and, i think some variable attribut wrong for date column and i cant insert any date values using that text box is am trying to insert it says invalid month
    0
  • 0
     cmd.CommandText = "CREATE TABLE con(id varchar2(20),name varchar2(50), createdDate DATE)"; // i dont know hw to create date datatype here

    Data is a keyword, so i renamed to createdDate.

    During insertion, what are the values you're sending to oracle db especially for the date column? Where & How are you entering date values?
    0
  • 0
    run time i have to insert the value, id = txtid.text, name=txtname.text and createddate=txtdate. run time user have to insert and submit the values
    0
  • 0
    on the click of the button insert all the three textboxes values should be insert into db. , id = txtid.text, name=txtname.text and createddate=txtdate. run time user have to insert and submit the values
    0
  • 0
    murali can you please guid eme in this. am eagerly waiting for your reply
    0
  • 0
    id = txtid.text, name=txtname.text and createddate=(DateTime)txtdate.text
    0
  • 0
    vidhya sri, but cannot implicitly convert string to date in oracle db know, so its throwing error.
    0
  • 0

    try like this,

    OracleDateTime ordate;

    If not

    can u mention what error you are getting?
    0
  • 0
    If you don't know the formatting,
    createdDate = DateTime.Parse(txtdate.text);

    If you know date format in db side,
    createdDate = DateTime.ParseExact(txtdate.text, format,CultureInfo.InvariantCulture);
    0
  • 1 short reply
    • 0
      what is your problem here?
      0