Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C# code for updating(stored procedure,DAL,BAL and UI)using listbox for items and textbox to update

Stored procedure
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_UpdateMainQ]

@MainQuestionId VARCHAR (50)

AS

UPDATE MAINQUESTION
SET SurveyId=@MainQuestionId
WHERE MainQuestion =MainQuestion


DAL code

C#
public int UpdateMainQuestion(string MainQuestion)
        {
            //using (SqlConnection connection = new SqlConnection(connectionInfo))
            //{
            //    connection.Open();
            //    SqlDataReader myReader = null;
            //    SqlCommand myCommand = new SqlCommand("p_UpdateMainQ", connection);
            //    myCommand.CommandType = CommandType.StoredProcedure;
            //    myCommand.Parameters.Add(new SqlParameter("@MainQuestion", MainQuestion));
            //    myReader = myCommand.ExecuteReader();

            //    if (myReader.Read())
            //    {

            //        //txtMQ.Text = myReader[1].ToString();


            //    }
            //}

            SqlConnection conn = new SqlConnection(connectionInfo);
            conn.Open();
            SqlCommand dCmd = new SqlCommand("p_UpdateMainQ", conn);
            dCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                //dCmd.Parameters.AddWithValue("@MainQuestionId", MainQuestionId);
               // dCmd.Parameters.AddWithValue("@SurveyId", 1);
                dCmd.Parameters.AddWithValue("@MainQuestion", MainQuestion);

                return dCmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                dCmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }


BAL code

SQL
public int UpdateMainQuestion(string MainQuestion)
      {
         return  MQDAL.UpdateMainQuestion(MainQuestion);
      }



UI code

C#
try
               {
                   string Ques = lstMainQ.SelectedItem.ToString();
                   txtMain.Text = Ques;
                   txtMain.Focus();
                   int Index = lstMainQ.SelectedIndex;
                   lstMainQ.Items.RemoveAt(lstMainQ.SelectedIndex);
                   String MainQuestion = txtMain.Text;
                   MainQuestionBAL MQBAL = new MainQuestionBAL();
                 int MQ=  MQBAL.UpdateMainQuestion(MainQuestion);
                 if (MQ.Equals(1))
                 {
                 }
Posted
Updated 25-May-12 10:23am
v2
Comments
Sebastian T Xavier 25-May-12 4:49am    
good question :)
What have you tried?
Nontobeko 25-May-12 5:19am    
Stored procedure
<pre lang="sql">set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_UpdateMainQ]

@MainQuestionId VARCHAR (50)

AS

UPDATE MAINQUESTION
SET SurveyId=@MainQuestionId
WHERE MainQuestion =MainQuestion</pre>

DAL code

public int UpdateMainQuestion(string MainQuestion)
{
//using (SqlConnection connection = new SqlConnection(connectionInfo))
//{
// connection.Open();
// SqlDataReader myReader = null;
// SqlCommand myCommand = new SqlCommand("p_UpdateMainQ", connection);
// myCommand.CommandType = CommandType.StoredProcedure;
// myCommand.Parameters.Add(new SqlParameter("@MainQuestion", MainQuestion));
// myReader = myCommand.ExecuteReader();

// if (myReader.Read())
// {

// //txtMQ.Text = myReader[1].ToString();


// }
//}

SqlConnection conn = new SqlConnection(connectionInfo);
conn.Open();
SqlCommand dCmd = new SqlCommand("p_UpdateMainQ", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
//dCmd.Parameters.AddWithValue("@MainQuestionId", MainQuestionId);
// dCmd.Parameters.AddWithValue("@SurveyId", 1);
dCmd.Parameters.AddWithValue("@MainQuestion", MainQuestion);

return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}

BAL code

<pre lang="sql">public int UpdateMainQuestion(string MainQuestion)
{
return MQDAL.UpdateMainQuestion(MainQuestion);
}</pre>


UI code

<pre lang="cs">try
{
string Ques = lstMainQ.SelectedItem.ToString();
txtMain.Text = Ques;
txtMain.Focus();
int Index = lstMainQ.SelectedIndex;
lstMainQ.Items.RemoveAt(lstMainQ.SelectedIndex);
String MainQuestion = txtMain.Text;
MainQuestionBAL MQBAL = new MainQuestionBAL();
int MQ= MQBAL.UpdateMainQuestion(MainQuestion);
if (MQ.Equals(1))
{
}</pre>

Nontobeko 25-May-12 5:26am    
Stored procedure
<pre lang="sql">set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_UpdateMainQ]

@MainQuestionId VARCHAR (50)

AS

UPDATE MAINQUESTION
SET SurveyId=@MainQuestionId
WHERE MainQuestion =MainQuestion</pre>

DAL code

public int UpdateMainQuestion(string MainQuestion)
{
//using (SqlConnection connection = new SqlConnection(connectionInfo))
//{
// connection.Open();
// SqlDataReader myReader = null;
// SqlCommand myCommand = new SqlCommand("p_UpdateMainQ", connection);
// myCommand.CommandType = CommandType.StoredProcedure;
// myCommand.Parameters.Add(new SqlParameter("@MainQuestion", MainQuestion));
// myReader = myCommand.ExecuteReader();

// if (myReader.Read())
// {

// //txtMQ.Text = myReader[1].ToString();


// }
//}

SqlConnection conn = new SqlConnection(connectionInfo);
conn.Open();
SqlCommand dCmd = new SqlCommand("p_UpdateMainQ", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
//dCmd.Parameters.AddWithValue("@MainQuestionId", MainQuestionId);
// dCmd.Parameters.AddWithValue("@SurveyId", 1);
dCmd.Parameters.AddWithValue("@MainQuestion", MainQuestion);

return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}

BAL code

<pre lang="sql">public int UpdateMainQuestion(string MainQuestion)
{
return MQDAL.UpdateMainQuestion(MainQuestion);
}</pre>


UI code

<pre lang="cs">try
{
string Ques = lstMainQ.SelectedItem.ToString();
txtMain.Text = Ques;
txtMain.Focus();
int Index = lstMainQ.SelectedIndex;
lstMainQ.Items.RemoveAt(lstMainQ.SelectedIndex);
String MainQuestion = txtMain.Text;
MainQuestionBAL MQBAL = new MainQuestionBAL();
int MQ= MQBAL.UpdateMainQuestion(MainQuestion);
if (MQ.Equals(1))
{
}</pre>

data is not updated from database so i dont know whether is the stored procedure or the way i call the methods
OriginalGriff 25-May-12 4:49am    
What have you tried?
Where are you stuck?
Nontobeko 25-May-12 5:19am    
Stored procedure
<pre lang="sql">set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_UpdateMainQ]

@MainQuestionId VARCHAR (50)

AS

UPDATE MAINQUESTION
SET SurveyId=@MainQuestionId
WHERE MainQuestion =MainQuestion</pre>

DAL code

public int UpdateMainQuestion(string MainQuestion)
{
//using (SqlConnection connection = new SqlConnection(connectionInfo))
//{
// connection.Open();
// SqlDataReader myReader = null;
// SqlCommand myCommand = new SqlCommand("p_UpdateMainQ", connection);
// myCommand.CommandType = CommandType.StoredProcedure;
// myCommand.Parameters.Add(new SqlParameter("@MainQuestion", MainQuestion));
// myReader = myCommand.ExecuteReader();

// if (myReader.Read())
// {

// //txtMQ.Text = myReader[1].ToString();


// }
//}

SqlConnection conn = new SqlConnection(connectionInfo);
conn.Open();
SqlCommand dCmd = new SqlCommand("p_UpdateMainQ", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
//dCmd.Parameters.AddWithValue("@MainQuestionId", MainQuestionId);
// dCmd.Parameters.AddWithValue("@SurveyId", 1);
dCmd.Parameters.AddWithValue("@MainQuestion", MainQuestion);

return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}

BAL code

<pre lang="sql">public int UpdateMainQuestion(string MainQuestion)
{
return MQDAL.UpdateMainQuestion(MainQuestion);
}</pre>


UI code

<pre lang="cs">try
{
string Ques = lstMainQ.SelectedItem.ToString();
txtMain.Text = Ques;
txtMain.Focus();
int Index = lstMainQ.SelectedIndex;
lstMainQ.Items.RemoveAt(lstMainQ.SelectedIndex);
String MainQuestion = txtMain.Text;
MainQuestionBAL MQBAL = new MainQuestionBAL();
int MQ= MQBAL.UpdateMainQuestion(MainQuestion);
if (MQ.Equals(1))
{
}</pre>


1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900