You must Sign In to post a response.
  • Category: ASP.NET

    How to call storedprocedure from windowsform

    how to call storedprocedure from windowsform
    pls reply me
    thanks in advance
  • #648284
    Hi sravan,

    Refer the below to call the storedprocedure from windows application..


    con.Open()
    Dim Sqlcmd As New SqlCommand
    Sqlcmd.Connection = con
    Sqlcmd.CommandType = CommandType.StoredProcedure
    Sqlcmd.CommandText = "ProcedureName"
    Dim DeleteP1 As New SqlParameter("@Name", SqlDbType.VarChar)
    DeleteP1.Size = 50
    DeleteP1.Direction = ParameterDirection.Input
    DeleteP1.Value = strName
    Sqlcmd.Parameters.Add(DeleteP1)
    Sqlcmd.ExecuteNonQuery()




    Hope this will help you.

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #648288
    Hi,
    Please check below code:


    try
    {
    SqlConnection conn = new SqlConnection("server=(local);database=pubs;UID=myUser;PWD=myPassword;");
    SqlDataAdapter MDA = new SqlDataAdapter("Name", conn);
    MDA.SelectCommand.CommandType = CommandType.StoredProcedure;
    MDA.SelectCommand.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 40));
    MDA.SelectCommand.Parameters["@name"].Value = Name.Trim();
    DataSet DS = new DataSet();

    MDA.Fill(DS, "Name");
    Console.WriteLine(MDA.SelectCommand.Parameters[1].Value + " Rows Found.");
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }

    Regards
    Mr.Rajesh
    Software Engg.

  • #648290
    hi,

    Try this code

    public class dallogin
    {
    string str = ConfigurationSettings.AppSettings["connectinstr"];

    public int dlogin(bologin boinfo)
    {
    SqlConnection con = new SqlConnection(str);
    con.Open();
    SqlCommand cmd = new SqlCommand("USP_LOGIN", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@username", boinfo.username);
    cmd.Parameters.AddWithValue("@password", boinfo.password);
    cmd.Parameters.AddWithValue("@outputs",boinfo.outputs);
    cmd.Parameters["@outputs"].Direction = ParameterDirection.Output;
    cmd.ExecuteReader();
    int rt = Convert.ToInt32(cmd.Parameters["@outputs"].Value);
    return rt;
    }
    }


    i hope this will work for you

    SqlCommand cmd = new SqlCommand("USP_LOGIN", con);
    cmd.CommandType = CommandType.StoredProcedure;

    at USP_LOGIN write the name of store procedure
    then commandtype as storedprocedure

    Thanks

  • #648300
    Please check the following code which will validate a user using Stored Procedure:

    Database Scripts
    ----------------


    CREATE TABLE TblUsers
    (UserID VARCHAR(50),
    LoginID VARCHAR(50),
    Password VARCHAR(50)
    )

    CREATE PROCEDURE CheckUser @loginid VARCHAR(50), @password VARCHAR(50) AS
    BEGIN
    SELECT userid FROM TblUsers WHERE LoginID = @loginid AND Password = @password
    END



    Form1.cs
    --------


    SqlConnection con;
    SqlCommand com;

    private void Form1_Load(object sender, EventArgs e)
    {
    con = new SqlConnection("Your Connection Strings goes here...");
    }

    private void button1_Click(object sender, EventArgs e)
    {
    if (ValidateUser(TxtLoginID.Text, TxtPassword.Text) != "")
    {
    MessageBox.Show("User Validated");
    }
    else
    {
    MessageBox.Show("Invalid User");
    }
    }

    public string ValidateUser(string LoginID, string Passwrod)
    {
    string UserID = "";
    try
    {
    com = new SqlCommand("CheckUser", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@loginid", LoginID);
    com.Parameters.AddWithValue("@password", Passwrod);
    con.Open();
    UserID = Convert.ToString(com.ExecuteScalar());
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    con.Close();
    }
    return UserID;
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #648372
    Hi,

    Check this code in which SP return values :



    SqlConnection conn = new SqlConnection("Data Source=localhost;Database=xyz;Integrated Security=SSPI");
    SqlCommand command = new SqlCommand("GetProducts", conn);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1;
    conn.Open();
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
    Console.WriteLine(reader["ProductName"]);
    }
    conn.Close();


    With Regards
    Kushal

  • #648508
    [Response removed by Admin. Read forum policies.]
    Regards
    Kamlesh Mohanty


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.