C
cally_kalpana
Guest
Hi
I am interested in finding out how can I create a helper class in order to connect to database and use stored procedure to for some operation. I would need to use different stored procedures but connecting to the same database, same table in my application.
I dont think writing the whole connect to database code at different section in my application is the right approach. This is my code.
try
{
using (SqlConnection connection = new SqlConnection(MainWindow.connstring))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("CheckWorkerIdv2", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@workerid", txtPasswordbox.Password));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
userexists = reader.GetString(0);
userid = reader.GetInt32(1);
}
//if (userexists != "") String.IsNullOrEmpty(userexists)
if (!String.IsNullOrEmpty(userexists))
{
txtPasswordbox.Background = new SolidColorBrush(Color.FromRgb(170, 255, 0));
Window parentWindow = Window.GetWindow(this);
StackPanel sp = (StackPanel)parentWindow.FindName("splogindetails");
sp.Visibility = Visibility.Visible;
TextBox fillusername = (TextBox)parentWindow.FindName("loggedintextBox");
fillusername.Text = userexists;
fillusername.Visibility = Visibility.Visible;
TextBox useridtxt = (TextBox)parentWindow.FindName("workeridfromlogin");
useridtxt.Text = userid.ToString();
Button logout = (Button)parentWindow.FindName("btnlogout");
logout.Visibility = Visibility.Visible;
Label lblcurrentuser = (Label)parentWindow.FindName("LoggedinDetails");
lblcurrentuser.Visibility = Visibility.Visible;
Ellipse startstopbtn1 = (Ellipse)parentWindow.FindName("startstopbtn");
startstopbtn1.Visibility = Visibility.Visible;
this.Visibility = Visibility.Collapsed;
Border br = (Border)parentWindow.FindName("brdbuttonLegend");
br.Visibility = Visibility.Visible;
StackPanel sp1 = (StackPanel)parentWindow.FindName("buttonLegend");
sp1.Visibility = Visibility.Visible;
}
else
{
txtPasswordbox.Background = new SolidColorBrush(Color.FromRgb(222, 0, 0));
Window parentWindow = Window.GetWindow(this);
TextBox fillusername = (TextBox)parentWindow.FindName("loggedintextBox");
fillusername.Text = " ";
//label1.content = userexists.tostring();
MessageBox.Show("Please enter the Correct Worker Id");
}
}
}
}
catch (Exception ex)
{
LogHelper.Log(LogTarget.File, ex.Message);
}
Kindly advice.
Continue reading...
I am interested in finding out how can I create a helper class in order to connect to database and use stored procedure to for some operation. I would need to use different stored procedures but connecting to the same database, same table in my application.
I dont think writing the whole connect to database code at different section in my application is the right approach. This is my code.
try
{
using (SqlConnection connection = new SqlConnection(MainWindow.connstring))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("CheckWorkerIdv2", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@workerid", txtPasswordbox.Password));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
userexists = reader.GetString(0);
userid = reader.GetInt32(1);
}
//if (userexists != "") String.IsNullOrEmpty(userexists)
if (!String.IsNullOrEmpty(userexists))
{
txtPasswordbox.Background = new SolidColorBrush(Color.FromRgb(170, 255, 0));
Window parentWindow = Window.GetWindow(this);
StackPanel sp = (StackPanel)parentWindow.FindName("splogindetails");
sp.Visibility = Visibility.Visible;
TextBox fillusername = (TextBox)parentWindow.FindName("loggedintextBox");
fillusername.Text = userexists;
fillusername.Visibility = Visibility.Visible;
TextBox useridtxt = (TextBox)parentWindow.FindName("workeridfromlogin");
useridtxt.Text = userid.ToString();
Button logout = (Button)parentWindow.FindName("btnlogout");
logout.Visibility = Visibility.Visible;
Label lblcurrentuser = (Label)parentWindow.FindName("LoggedinDetails");
lblcurrentuser.Visibility = Visibility.Visible;
Ellipse startstopbtn1 = (Ellipse)parentWindow.FindName("startstopbtn");
startstopbtn1.Visibility = Visibility.Visible;
this.Visibility = Visibility.Collapsed;
Border br = (Border)parentWindow.FindName("brdbuttonLegend");
br.Visibility = Visibility.Visible;
StackPanel sp1 = (StackPanel)parentWindow.FindName("buttonLegend");
sp1.Visibility = Visibility.Visible;
}
else
{
txtPasswordbox.Background = new SolidColorBrush(Color.FromRgb(222, 0, 0));
Window parentWindow = Window.GetWindow(this);
TextBox fillusername = (TextBox)parentWindow.FindName("loggedintextBox");
fillusername.Text = " ";
//label1.content = userexists.tostring();
MessageBox.Show("Please enter the Correct Worker Id");
}
}
}
}
catch (Exception ex)
{
LogHelper.Log(LogTarget.File, ex.Message);
}
Kindly advice.
Continue reading...