Krishna Garad
posted
825 posts
since
Dec 19, 2009
from
Hyderabad
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
try this 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();
|
|
|
|
|
Mark as "This Is Correct Answer" if helps you.
|
|
|
|
|
|
Vulpes
posted
5113 posts
since
Feb 28, 2011
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
You're lacking the names of the columns into which to insert the corresponding values in the database table:
cmd.CommandText = "INSERT INTO test (amount, description) VALUES (@amount, @description)";
|
|
|
|
|
|
Mohammad Rashed Obaidat
posted
5 posts
since
Dec 12, 2010
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
Krishna Garad, I tried what you said but nothing happened the data is not inserted on the Database, but thank you anyway.
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.
|
|
|
|
|
|
Satyapriya Nayak
posted
1902 posts
since
Mar 24, 2010
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
Hi Mohammad,
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Register.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:TextBox ID="txt_amount" runat="server"></asp:TextBox> <p> <asp:TextBox ID="rtxt_description" runat="server"></asp:TextBox> </p> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </form> </body> </html>
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 Obaidat
posted
5 posts
since
Dec 12, 2010
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
Dear Satyapriya Nayak, the code you gave me and the code i wrote can work on a website, but the problem is I'm trying to insert the data on a Windows Application Form and I can't figure why it doesn't insert any data
|
|
|
|
|
|
Vulpes
posted
5113 posts
since
Feb 28, 2011
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
If you change:
cmd.ExecuteNonQuery();
to:
int rowsAffected = cmd.ExecuteNonQuery();
and then either put a breakpoint on that line or do a MessageBox.Show on rowsAffected, do you get a non-zero value?
|
|
|
|
|
|
Zuber Kazi
posted
17 posts
since
Jan 10, 2012
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
try this sqlcommand cmd=new sqlcommand("insert into test values('"+Convert.ToInt64(txt_amount.Text)+"','"+rtxt_description.Text+"')",con); cmd.ExecuteNonQuery();
|
|
|
|
|
|
Mohammad Rashed Obaidat
posted
5 posts
since
Dec 12, 2010
from
|
|
Re: Insert Data into SQL DataBase
|
|
|
|
|
|
|
|
|
|
|
I tried every thing you said, but nothing is working.
I'm afraid that the problem might be from the visual studio itself
|
|
|
|
|
|