Avatar of babubhai
babubhai

asked on 

Help & Urgent Please

Hello Experts, Im learning C# and trying to design one application...Im on initial level of learning.
Could you help me how to design the application....
I have a store procedure in mssql database...which is below....
CREATE PROCEDURE [dbo].[AddModifySP]
(
      @JobID int
)
AS
      SET NOCOUNT ON;
SELECT
[CLIENT NAME],
[JOB NUMBER],
[JOB ADDRESS],
[CONTACT NUMBER],
[JOB TYPE],
[JOBID]
FROM
DBO.[CLIENT TABLE]
WHERE
[JOBID] = @JobID

Could you tell me how can I write code to access the data from storeprocedure in C# application and where I need to write it.? thanks in advance

Also I have the designed form which has
JobID, ClientName, JobNumber, JobAddress, JobType Field.......

Could you tell me how can I pull the data for particular client...and where I need to write connectionstring in application....

Also I have a dataSet which has storeProcesure
.NET Programming

Avatar of undefined
Last Comment
babubhai
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Avatar of babubhai
babubhai

ASKER

before I was using following codes: but it's not working Sir

        private void Add2_Load(object sender, EventArgs e)
        {
            string strCon = @"Data Source=FARRUKH\SQLEXPRESS;Initial Catalog=DLCallLog;Integrated Security=True";
            SqlConnection myConnection = new SqlConnection(strCon);
            myConnection.Open();


            //Assign the query command and connection to the SqlCommand object
            SqlCommand myCommand = new SqlCommand("AddModifySP", myConnection);
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.StoredProcedure;
         
            //here is the value For "JobID"
            int job1 = 1;

            myCommand.Parameters.AddWithValue("@JobID" , job1);


            //Create DataAdapter to fill your dataSet or dataTable
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = myCommand;

            //You can use DataSet or DataTable as your datasource
            //Using DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, "Results");
            myConnection.Close();
            myConnection.Dispose();
            myCommand.Dispose();
}
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

What error are you getting?
Avatar of babubhai
babubhai

ASKER

Nothing. Even Application is building successfully,
Also I bind the data through in property.
Above code which i sent you before i wrote in Form_Load.....
OR
do i need to write these codes on another place.
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

If there is no error then it should work, try this link too:
http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c13537/

Avatar of babubhai
babubhai

ASKER

Sir, I have Following Text Fields in my Form (Add2)
JobID, ClientName, JobNumber, JobAddress, ContactNumber and JobType.
Could you tell me Sir, How I can Bind above fields with my DataSet

DataSetN Name is DLDataSet and Im using StoreProcedure Name is AddModifySP....
StoreProcedure is:---
CREATE PROCEDURE [dbo].[AddModifySP]
(
      @JobID int
)
AS
      SET NOCOUNT ON;
SELECT
[CLIENT NAME],
[JOB NUMBER],
[JOB ADDRESS],
[CONTACT NUMBER],
[JOB TYPE],
[JOBID]
FROM
DBO.[CLIENT TABLE]
WHERE
[JOBID] = @JobID
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Use this code snippet and modify it as per your requirements.
Let me know if you get any errors.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
using System.Configuration;
using System.Data.SqlClient;
 
public class Form1 : Form {
    public Form1() {
        InitializeComponent();
 
        DataSet ds = CreateDataSet();
 
        textBox1.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
    }
 
    private DataSet CreateDataSet() {
        string customers = "SELECT * FROM Products";
        DataSet ds = new DataSet();
 
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            SqlDataAdapter da = new SqlDataAdapter(customers, con);
 
            da.Fill(ds, "Products");
        }
 
        return ds;
    }
    private void InitializeComponent() {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBox1.Location = new System.Drawing.Point(12, 12);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(370, 20);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(394, 44);
        this.Controls.Add(this.textBox1);
        this.Name = "Form1";
        this.Text = "SimpleDataBinding";
        this.ResumeLayout(false);
        this.PerformLayout();
 
    }
 
 
 
    private System.Windows.Forms.TextBox textBox1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

Open in new window

Avatar of babubhai
babubhai

ASKER

I want to ask one more thing Sir,
Before I wrote all codes in Add2.cs (on form load)....
or just move my codes to Program.cs or create another .cs file.
Avatar of babubhai
babubhai

ASKER

Getting Error: Add1.Add2.CreateDataSet()': not all code paths return a value

Showing Error on Line is :          private DataSet CreateDataSet()

namespace Add1
{
    public partial class Add2 : Form
    {
        public Add2()
        {
            InitializeComponent();
            DataSet ds = CreateDataSet();
            JobID.DataBindings.Add("Text", ds.Tables["Results"], "JobID");
        }

        private DataSet CreateDataSet()
        {
            string strCon = @"Data Source=FARRUKH\SQLEXPRESS;Initial Catalog=DLCallLog;Integrated Security=True";
            SqlConnection myConnection = new SqlConnection(strCon);
            myConnection.Open();


            //Assign the query command and connection to the SqlCommand object
            SqlCommand myCommand = new SqlCommand("AddModifySP", myConnection);
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.StoredProcedure;

            //here is the value For "JobID"
            int job1 = 1;

            myCommand.Parameters.AddWithValue("@JobID", job1);


            //Create DataAdapter to fill your dataSet or dataTable
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = myCommand;

            //You can use DataSet or DataTable as your datasource
            //Using DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, "Results");
            myConnection.Close();
            myConnection.Dispose();
            myCommand.Dispose();
        }
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

I don't think the name will matter as long as you refrence the correct code file.
You can create a new file if you want. I would suggest that you create a copy of your existing code (Add2.cs) and then make changes to it. This way you will have a back up in case something goes wrong.
Also, please do not refer to me as SIR, because I am not, call me faheem :-)
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

You are missing on returning the dataset from the function CreateDataSet:
return ds;

Check the code snippet again, its there.
Avatar of babubhai
babubhai

ASKER

Faheem, its working IF i just add JobID to databindings,,,, when Im going to add another like clientname its giving me error which is on Program.cs:
File Name is Add2.cs (below codes belong to Add2.cs)
public Add2()
        {
            InitializeComponent();
            DataSet ds = MydataSet();
            JobID.DataBindings.Add("Text", ds.Tables["Results"], "JobID");
            ClientName.DataBindings.Add("Text", ds.Tables["Results"], "ClientName");
        }

Error on Line is :             Application.Run(new Add2());
File Name : Program.cs
static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Add2());
        }
ASKER CERTIFIED SOLUTION
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of babubhai
babubhai

ASKER

it;s working Faheem.
I want to ask you another thing.

Basically I have two C# Application both are Attached togather.
One is Add1 and Second is Add2....
Whatever i asked you in previous conversation it was belong to Add2.

My Add1 application is working fine .... but there is an integer is called JobID and Im getting from another dataset....

I want to transfer that JobID Value From Add1 to Add2.....
as you remember Im using  I was using below code for passing value
int job1 = 1;
now I want to transfer JobID from Add1 to Add2

Wating for your valueable reply. Thanks
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

JobId will need to be public. You can access it as: Add1.JobId in Add2
Avatar of babubhai
babubhai

ASKER

I have below code in Add1:
private void cLIENT_TABLEDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
BindingManagerBase bm = cLIENT_TABLEDataGridView.BindingContext[cLIENT_TABLEDataGridView.DataSource, cLIENT_TABLEDataGridView.DataMember];
            DataRowView view = (DataRowView)bm.Current;
            DLCallLogDataSet.CLIENT_TABLERow row = (DLCallLogDataSet.CLIENT_TABLERow)view.Row;
           
            MessageBox.Show(row.JobID+"  "+ "Name : "+row.CLIENT_NAME+"  Address : "+row.JOB_ADDRESS);
Add1.Add2 MyNewForm = new Add1.Add2();
            MyNewForm.ShowDialog();

            int MyJobID = row.JobID;
}
---------------------------------------------------------
now the above integer value i want to transfer....
Could you tell me faheem how can I public my integer?
Avatar of babubhai
babubhai

ASKER

problem is fixed
Avatar of babubhai
babubhai

ASKER

thanks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo