1 Replies - 1260 Views - Last Post: 23 August 2011 - 08:30 PM

#1 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Can't insert using Stored Procedure in MS SQL

Posted 23 August 2011 - 08:25 PM

I tried to insert database using MS SQL , i write my code in C# then i added new data source which is xlogin.mdf file then i create a stored procedure in the SQL named "insrt" but when i run , there is no new record inserted even there is no errors neither warnings .. Actually i looked at the table via the Microsoft Visual C# as i added in but nothing appeared ! this is my code :

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand comm = new SqlCommand();
            conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\xLogin.mdf;Integrated Security=True;Connect Timeout=30;User Instance = True";
            conn.Open();
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = "SELECT max(ID) FROM xLogin";
            int max = (int)(comm.ExecuteScalar()) + 1;
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = "insrt";
            comm.Parameters.Add("@id", SqlDbType.Int).Value = max.ToString();
            comm.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = textBox1.Text;
            comm.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = textBox2.Text;
            comm.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = textBox3.Text;
            comm.ExecuteNonQuery();
            MessageBox.Show("DONE !");
            conn.Close();
        }




Thanks in advance

This post has been edited by Private_Basim: 23 August 2011 - 08:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't insert using Stored Procedure in MS SQL

#2 Private_Basim   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 13
  • Joined: 26-July 11

Re: Can't insert using Stored Procedure in MS SQL

Posted 23 August 2011 - 08:30 PM

Taking into consideration that when i execute the stored procedure "insrt" from SQL and put some values to the parameters then refresh , the table seems to be have a new record .. So everything is allright .. This is my stored procedure code :

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insrt] @id int, @username varchar(50), @password varchar(50), @email varchar(50)
AS
INSERT INTO xLogin(ID,Username,Password,Email) VALUES (@id,@username,@password,@email)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1