saving a record to an sql database
Asked By SWGMJS Simelane
10-Aug-05 02:40 AM
I am new to VB.Net and I want to save a record to an SQL database the vb6.0 code does not work
Please help
swg
To insert a record into an sql database
Asked By Pinky G
10-Aug-05 03:49 AM
Hi, You can insert a record into sql database by using a connection object and command objet. Try this way
Dim cnn as SqlConnection
dim cmd as SqlCommand
cnn = new SqlConnection(<Please mention Connection string for your database>)
cnn.Open()
cmd = new sqlCommand("insert into <tablename> values (" & <value to insert>" & ")", cnn)
cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
Thnks Hope this solves your problem
updating /inserting a record
Asked By suneetha nambari
10-Aug-05 03:57 AM
hi
Are u inserting a record/updating it? into sql server
Check out the syntax for update table
Regards
Suneetha
vb net & sqlserver
Asked By Arulanantham Subramaniam
10-Aug-05 08:33 AM
refer this site..
ADO.NET
http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/adoplusoverview.aspx
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
http://www.murach.com/books/adon/download.htm
ADO
http://www.functionx.com/ado/Lesson05.htm
arul
SQL Server as your database, you can do the following: SqlConnection sqlConnection = new SqlConnection("YourConnectionString"); SqlCommand sqlCommand = ("", sqlConnection); int userCount; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = "SELECT COUNT(*) FROM [dbo].[YourUsersTable] WHERE [UserName] = @UserName sqlCommand.Parameters.Add("@UserName", SqlDbType.VarChar, 20); sqlCommand.Parameters["@UserName"] = YourUserNameHere; sqlConnection.Open(); userCount = Convert.ToInt32(sqlCommand.ExecuteScalar
is for SQL Server, I suggest using SqlParameter with your SqlCommand: SqlConnection sqlConnection = new SqlConnection("YourConnectionString"); SqlCommand sqlCommand = new SqlCommand("YourSPHere", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.Add("@Parameter1", SqlDataType.Int); sqlCommand.Parameters["@Parameter1"].value = yourStringValueHere
Get the connectionstring from the webconfig and declare a global SqlConnection "SqlConnection" public static string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; public SqlConnection SqlConnection = new SqlConnection(connectionString); / / Declare a global SqlDataAdapter SqlDataAdapter public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(); / / Declare a global SqlCommand SqlCommand public SqlCommand SqlCommand = new SqlCommand(); protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e) { / / Populate
many arguments specified> > . protected void BtnSend_Click(object sender, EventArgs e) { SqlConnection sqlconnection = new SqlConnection("Data source = NDEKEZIDANIEL \ SQLEXPRESS;Initial catalog = ECASH;integrated security = true"); SqlCommand sqlcommand = new SqlCommand("transfertcash", sqlconnection); sqlcommand.Parameters.Add("@numb", SqlDbType.BigInt).Value = Int64.Parse(TextBoxBenTel.Text Trim()); sqlcommand.Parameters.Add("@montant", SqlDbType.Int).Value = Int32.Parse(TextBoxMontant.Text.Trim()); sqlcommand.Parameters.Add("@numtel", SqlDbType.BigInt).Value = Session["num_tel"]; sqlcommand.Parameters
True; " ; / / string strcon = "Server = RAJA \ SQLEXPRESS;Database = ram;Trusted_Connection = True;"; SqlCommand cmdinserted = new SqlCommand (); SqlConnection con = new SqlConnection (strcon); cmdinserted.CommandText = "Insert into tooto VALUES ( '" + words[0].ToString CommandType = CommandType .Text; cmdinserted.Connection = con; try { con.Open(); cmdinserted.ExecuteNonQuery(); con.Close(); } catch ( ArgumentOutOfRangeException ex) { Label2.Text = ex.Message.ToString True; " ; / / string strcon = "Server = RAJA \ SQLEXPRESS;Database = ram;Trusted_Connection = True;"; SqlCommand cmdinserted = new SqlCommand (); SqlConnection con = new SqlConnection (strcon); cmdinserted.CommandText = "Insert into punya VALUES ( '" + T1.Text + "')" ; cmdinserted CommandType = CommandType .Text; cmdinserted.Connection = con; try { con.Open(); cmdinserted.ExecuteNonQuery(); con.Close(); } catch ( Exception ex) { Label2.Text = ex.Message.ToString
sql server sqlcommand.executenonquery() result show hello i have executed a query in sqlserver. using cmd.executeNonQuery and it returns -1 in my web page. when i returns the number of rows affected. syntax: public override int ExecuteNonQuery() follow http: / / msdn.microsoft.com / en-us / library / system.data.sqlclient.sqlcommand.executenonquery.aspx Hi always try to use the Try Catch Final the Error and Show to the User. Do like this SqlConnection connection = null ; Byte [] imgByte = null ; if (imgUpload.PostedFile ! = null && imgUpload into DB string con = ConfigurationManager .ConnectionStrings[ "TESTDB" ].ConnectionString; connection = new SqlConnection (con); connection.Open(); try { string Insert_Query = "insert into empdetails(empname, empimg) values(@en, @eimg) select @@identity" ; SqlCommand cmd = new SqlCommand (Insert_Query, connection); cmd.Parameters.Add( new SqlParameter ( "@en" , txtEName.Text
True; " ; / / string strcon = "Server = RAJA \ SQLEXPRESS;Database = ram;Trusted_Connection = True;"; SqlCommand cmdinserted = new SqlCommand (); SqlConnection con = new SqlConnection (strcon); cmdinserted.CommandText = "select*from jms" ; cmdinserted.CommandTimeout = 0; cmdinserted CommandType = CommandType .Text; cmdinserted.Connection = con; try { con.Open(); cmdinserted.ExecuteNonQuery(); con.Close(); } catch ( Exception ex) { Label1.Text = ex.Message.ToString True; " ; / / string strcon = "Server = RAJA \ SQLEXPRESS;Database = ram;Trusted_Connection = True;"; SqlCommand cmdinserted = new SqlCommand (); SqlConnection con = new SqlConnection (strcon); cmdinserted.CommandText = "select * from jms" ; / / Keep space between select CommandType = CommandType .Text; cmdinserted.Connection = con; try { con.Open(); / / cmdinserted.ExecuteNonQuery(); / / No need to ExecuteNonQuery. . . / / Normally we use ExecuteNonQuery when you
exception string StrConnectionString = ConfigurationManager.AppSettings["MyDbConn1"].ToString(); SqlConnection conn = new SqlConnection(StrConnectionString); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; SqlCommand command = new SqlCommand("spToInsertUserinfo", conn); cmd.Parameters.Add( new SqlParameter("@FristName", txtFristName.Text Parameters.Add(new SqlParameter("@Password", txtPss.Text)); conn.Open(); cmd.ExecuteNonQuery(); throwing execption "{"ExecuteNonQuery: CommandText property has not been initialized"}" please help This looks your code to this. . string StrConnectionString = ConfigurationManager.AppSettings[ "MyDbConn1" ].ToString(); SqlConnection conn = new SqlConnection (StrConnectionString); SqlCommand command = new SqlCommand ( "spToInsertUserinfo" , conn); cmd.CommandType = CommandType.StoredProcedure; cmd