i have done some coding but it give me error (Could not find stored procedure) when i am trying to save data into table .
// Bellow is my code//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ABDHMS.Accounts
{
public partial class AccountSetup : Form
{
public AccountSetup()
{
InitializeComponent();
SetNew();
}
private void SaveAccount()
{
try
{
Conn obCon = new Conn();
SqlConnection ob = new SqlConnection(obCon.strCon);
SqlCommand cmd = new SqlCommand("tblAccounts", ob);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@AccNo", SqlDbType.Int);
cmd.Parameters.Add("@AccName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@AccType", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@OpeningBalance", SqlDbType.Float);
cmd.Parameters.Add("@Notes", SqlDbType.VarChar, 255);
cmd.Parameters[0].Value = txtID.Text;
cmd.Parameters[1].Value = txtAccName.Text;
cmd.Parameters[2].Value = cmbType.Text;
cmd.Parameters[3].Value = txtBalance.Text;
cmd.Parameters[4].Value = txtNotes.Text;
ob.Open();
cmd.ExecuteNonQuery();
ob.Close();
MessageBox.Show("Account Saved Successfully!","Successfull",MessageBoxButtons.OK,MessageBoxIcon.Information);
SetNew();
}
catch(Exception error)
{
MessageBox.Show("Failed to Save Account! "+error.Message.ToString(),"Failed",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
private void SetNew()
{
txtAccName.Text = "";
txtID.Text = "";
txtNotes.Text = "";
txtBalance.Text = "0";
cmbType.SelectedIndex = 0;
btnSave.Enabled = true;
btnEdit.Enabled = false;
GenerateID();
GridLoad();
}
private void GenerateID()
{
Conn obcon = new Conn();
SqlConnection ob = new SqlConnection(obcon.strCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = ob;
SqlCommand cmd = da.SelectCommand;
cmd.CommandText = "Select isnull(max(AccNo),0)+1 as AccNo from tblAccounts";
cmd.CommandType = CommandType.Text;
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtID.Text = dt.Rows[0]["AccNo"].ToString();
}
}
private void GridLoad()
{
Conn obcon = new Conn();
SqlConnection ob = new SqlConnection(obcon.strCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = ob;
SqlCommand ds = da.SelectCommand;
ds.CommandText = "SELECT* FROM tblAccounts";
ds.CommandType = CommandType.Text;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt;
}
private void btnClose_Click(object sender, EventArgs e)
{
Hide();
}
private void btnNew_Click(object sender, EventArgs e)
{
SetNew();
}
private void txtBalance_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveAccount();
}
private void btnEdit_Click(object sender, EventArgs e)
{
SaveAccount();
}
}
}
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj Kumar ChoudharyPosted Mar 23, 2015, 12:41 PM
For this You can use Sp_helptext SP_Name.
Sp_helptext tblAccounts
this cmd will return the definition of your SP. if your stored procedure is exist then please check connection string is user has permission to access SP
ISLAM ForEveryOnePosted Mar 26, 2015, 5:30 AM
Tom MohanPosted Mar 23, 2015, 11:56 AM
Khargesh RajputPosted Mar 23, 2015, 11:47 AM