C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




insert query


Posted Date: 28 Aug 2008      Total Responses: 7

Posted By: Lanka Harini       Member Level: Silver     Points: 1


Hii, can u tell me how to write insert query in the default.cs page without using storeprocedure



Responses

Author: Geetha    28 Aug 2008Member Level: GoldRating:     Points: 6
Try like this. Instead of using stored procedure use the insert query directly.

using System.Data.SqlClient;

string sInsert = "insert into EMPDETAILS(EID,EName) values(" + txtID.Text + ",'" + txtName.Text + "')";

SqlConnection connection = new SqlConnection("Database=Master;Server=ws333;User ID=sa;Password=sa123;");
SqlCommand sqlCmd = new SqlCommand();
connection.Open();
sqlCmd.Connection = connection;
sqlCmd.CommandText = sInsert;
sqlCmd.ExecuteNonQuery();
connection.Close();



Author: vipul    28 Aug 2008Member Level: DiamondRating:     Points: 6
hi,
you used this way
using System.Data.SqlClient;

string sInsert = "insert into category(catID,catName) values(" + txtcatID.Text + ",'" + txtcatName.Text + "')";

SqlConnection connection = new SqlConnection("Database=test;Server=test;User ID=sa;Password=test12354;");
SqlCommand sqlCmd = new SqlCommand();
connection.Open();
sqlCmd.Connection = connection;
sqlCmd.CommandText = sInsert;
sqlCmd.ExecuteNonQuery();
connection.Close();

vipul,
http://dongavipul.blogspot.com



Author: ANIL PANDEY    28 Aug 2008Member Level: DiamondRating:     Points: 6
hi,


Refer this Code...

protected void btnApplay_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConStr);
SqlCommand cmd = new SqlCommand();
DateTime dtFrom = Convert.ToDateTime(txtFromDate.Text.ToString() );
DateTime dtTo = Convert.ToDateTime(txtTodate.Text.ToString());
TimeSpan ts;
UID = Convert.ToInt32( Session["UID"].ToString());
string strQuery = string.Empty;
string strStatus = "Pending";
double ldblDays;
int lintResult;

ts = Convert.ToDateTime(dtTo).Subtract(dtFrom);
ldblDays = ts.Days + 1;

//Checking for the hal/Full Day
if( (ddlHF.SelectedItem.Text == "Half") && (ldblDays > 1))
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "You can not select more than one day as Half day.";
ddlHF.Focus();
}
else
{
if ((ddlHF.SelectedItem.Text == "Half") && (ldblDays == 1))
{
ldblDays = 0.5;
}

if (CheckApplication(ldblDays))
{
con.Open();
strQuery = "insert into tbLeaveDetails values(" + UID + ",'" + txtFromDate.Text.Trim() + "','" + txtTodate.Text.Trim() + "'," + ldblDays + ",'" + ddlType.SelectedItem.Text + "','" + strStatus + "')";
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
lintResult = cmd.ExecuteNonQuery();

if (lintResult > 0)
{
lblMsg.ForeColor = System.Drawing.Color.Green;
lblMsg.Text = "Leave sent for approval.";
txtFromDate.Text = "";
txtTodate.Text = "";
ddlHF.SelectedIndex = 0;
ddlType.SelectedIndex = 0;
fillGrid();
}
else
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "Error while sending leave request.";
}
}
// Closing the Connection
con.Close();
cmd.Dispose();
con.Dispose();
}

}

Thanks
Anil Pandey



Author: Sidewinder2    28 Aug 2008Member Level: GoldRating:     Points: 6
Hi,

try this

public static void Main() {
SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
mySqlConnection.Open();
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"INSERT INTO Customers (" +
" CustomerID, CompanyName, ContactName" +
") VALUES (" +
" @CustomerID, @CompanyName, @ContactName" +
")";
mySqlCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5);
mySqlCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40);
mySqlCommand.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30);
mySqlCommand.Parameters["@CustomerID"].Value = "J4COM";
mySqlCommand.Parameters["@CompanyName"].Value = "J4 Company";
mySqlCommand.Parameters["@ContactName"].IsNullable = true;
mySqlCommand.Parameters["@ContactName"].Value = DBNull.Value;
mySqlCommand.ExecuteNonQuery();
Console.WriteLine("Successfully added row to Customers table");

mySqlConnection.Close();
}



Author: Athira Appukuttan    28 Aug 2008Member Level: DiamondRating:     Points: 6
Hi..


using System.Data.SqlClient;

public void save_Records()
{
string strFname, strLname, strTitle, strCity, strQuey;
SQLHelper addEmployee = new SQLHelper();
strFname = txtFName.Text;
strLname = txtLName.Text;
strTitle = txtTitle.Text;
strCity = txtCity.Text;
if (Session["EmpId"] == null)
{
strQuey = "insert into employees(firstname,lastname,title,city)values('" + strFname + "','" + strLname + "','" + strTitle + "','" + strCity + "')";
}
else
{
strQuey = "update employees set firstname='" + strFname + "',lastname='" + strLname + "',title='" + strTitle + "',city='" + strCity + "' where employeeid='" + Session["EmpId"] + "'";
}
int rec=addEmployee.ExecuteNonQuery(strQuey);
if (rec > 0)
{
Response.Redirect("GridViewNew.aspx");
}



SQLHelper.cs.txt
Author: Vivek Sharma    11 Sep 2008Member Level: GoldRating:     Points: 3
Hi,
Insert query on .cs page

insert into tablename (firstname,lastname,phonenumber) values
('"+txtFirstname.Text+"','"+txtLastname.Text+"',"+txtPhonenumber+"))

Regards
Vivek Sharma


Author: Ravi Kiran Nedunuri    24 Sep 2008Member Level: GoldRating:     Points: 6
string sql = "insert into students(sname,class,fees) values('" + name.Text + "'," + cls.Text + "," + fees.Text + ")";
//Response.Write(sql);
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ToString());
try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
BindData();
if (i > 0)
{
Response.Redirect("AddRecords.aspx");
//Response.Write("Record Sucessfully Added....");
}
else
{
Response.Write("please try again");
}
}
catch (Exception ex)
{
throw;
}
finally
{
cn.Close();
}

you use the above code for a query in default.aspx.cs for that you need to specify in for command object whether you are using query or stored procedure by using
cmd.CommandType = CommandType.Text;

Regards
N.RaviKiran



Post Reply
You must Sign In to post a response.
Next : i would like to get single row from multiple rows from sp
Previous : Date Difference in oracle
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use