Dear all,
I try to insert data into my Database but when I click the button nothing happen I tried to use select statement instead of insert statement and it works well, but when I try ti update or insert data nothing happens, could anybody help me with that?
This is the code:
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\nobel.mdf;Integrated Security=True;User Instance=True";
con.Open();
cmd.CommandText = "INSERT INTO test VALUES (@amount, @description)";
cmd.Parameters.AddWithValue("@amount", Convert.ToInt64(txt_amount.Text));
cmd.Parameters.AddWithValue("@description", rtxt_description.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Loading
Mohammad Rashed ObaidatPosted Feb 8, 2012, 11:29 AM
Zuber KaziPosted Feb 8, 2012, 3:02 AM
sqlcommand cmd=new sqlcommand("insert into test values('"+Convert.ToInt64(txt_amount.Text)+"','"+rtxt_description.Text+"')",con);
cmd.ExecuteNonQuery();
VulpesPosted Feb 7, 2012, 9:58 AM
Mohammad Rashed ObaidatPosted Feb 7, 2012, 9:53 AM
Satyapriya NayakPosted Feb 7, 2012, 9:49 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Register.WebForm1" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Register_using_param
{
public partial class WebForm1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
string str;
SqlCommand cmd;
protected void Register_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str="INSERT INTO test VALUES (@amount, @description)";
cmd = new SqlCommand(str,con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@amount", Convert.ToInt64(txt_amount.Text));
cmd.Parameters.AddWithValue("@description", rtxt_description.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Records Inserted");
}
}
}
Thanks
Mohammad Rashed ObaidatPosted Feb 7, 2012, 9:40 AM
Vulpes, you have to write the column names if you want to insert data into specific fields on the Database, and I tried it and it didn't help, but thank you anyway.
VulpesPosted Feb 7, 2012, 9:31 AM
Krishna GaradPosted Feb 7, 2012, 8:55 AM
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\nobel.mdf;Integrated Security=True;User Instance=True";);
SqlCommand cmd = new SqlCommand( "INSERT INTO test VALUES (@amount, @description)",con);
con.Open();
cmd.Parameters.AddWithValue("@amount", Convert.ToInt64(txt_amount.Text));
cmd.Parameters.AddWithValue("@description", rtxt_description.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();