Members I have a question how to connect to sql server tables of a database with this proect's data.
Can you suggest .
Members you can see that inside the bin\debug\ContactManager.state now in this ContactManager.state is the file where the data is getting stored when I am running the proj.
There is a _stateFile in the ContactRepository.cs where the values are getting stotred.This variable is later on used for all the operations.
My question is how to connect to sql server instead to this ContactManager.state file
now inside contactrepository.cs file
<code> - using System;
-
using System.Collections.Generic;
-
using System.IO;
-
using System.Linq;
-
using System.Runtime.Serialization.Formatters.Binary;
-
-
namespace ContactManager.Model
-
{
-
public class ContactRepository
-
{
-
private List<Contact> _contactStore;
-
private readonly string _stateFile;
-
-
public ContactRepository()
-
{
-
_stateFile = Path.Combine(
-
AppDomain.CurrentDomain.BaseDirectory,
-
"ContactManager.state"
-
);
-
-
Deserialize();
-
}
-
-
public void Save(Contact contact)
-
{
-
if (contact.Id == Guid.Empty)
-
contact.Id = Guid.NewGuid();
-
-
if (!_contactStore.Contains(contact))
-
_contactStore.Add(contact);
-
-
Serialize();
-
}
-
-
public void Delete(Contact contact)
-
{
-
_contactStore.Remove(contact);
-
Serialize();
-
}
-
-
public List<Contact> FindByLookup(string lookupName)
-
{
-
IEnumerable<Contact> found =
-
from c in _contactStore
-
where c.LookupName.StartsWith(
-
lookupName,
-
StringComparison.OrdinalIgnoreCase
-
)
-
select c;
-
-
return found.ToList();
-
}
-
-
public List<Contact> FindAll()
-
{
-
return new List<Contact>(_contactStore);
-
}
-
-
private void Serialize()
-
{
-
using (FileStream stream =
-
File.Open(_stateFile, FileMode.OpenOrCreate))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
formatter.Serialize(stream, _contactStore);
-
}
-
}
-
-
private void Deserialize()
-
{
-
if (File.Exists(_stateFile))
-
{
-
using (FileStream stream =
-
File.Open(_stateFile, FileMode.Open))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
-
_contactStore =
-
(List<Contact>)formatter.Deserialize(stream);
-
}
-
}
-
else _contactStore = new List<Contact>();
-
}
-
}
-
}
.
</code>
now in this file you can see that - _stateFile = Path.Combine(
-
AppDomain.CurrentDomain.BaseDirectory,
-
"ContactManager.state"
-
is there .
Now to connect it to the sqlserver what I have to do is instead of the binary file "ContactManager.state " ....I want a it to be connected to a *.mdf .
How to do this please suggest me .
This is an open source project.
for the detail explanation please see the two attached files with this question which have attached with this question.
The code can be downloaded from the website:
1. Go to www.informit.com/title/9780672329715.
2. Click Downloads.
3. Click one of links that appear, and the download should start automatically.
17 9251 TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
The tag is [code] not <code>
My question is how to connect to sql server instead to this ContactManager.state file
now inside contactrepository.cs file
Until someone with more experience can offer more targeted advice I can point you toward these:
Database How-to parts 1 and 2 Database tutorial Part 1 Database tutorial Part 2
I know the sql data connection to the windows forms.
Like the following
<code>
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=STUDENT-1D40FC5\\SQLEXPRESS; Initial Catalog=LibraryMgnt;Integrated Security=SSPI");
string query = "insert into BookDetails(Title,Author,Publisher,Subject,No_Of_C opies)values('" + title.Text + "','" + author.Text + "','" + textBox3.Text + "','" + subject.Text + "','" + no.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
MessageBox.Show("The Book Details are saved.");
while (reader.Read())
{
}
reader.Close();
}
</code>
I want to know how to insert to the _stateFile in the code.
As if you see the full project and download it then you will see that it is using a binary file for the purpose of database connection and insert,update,delete.
So in place
<code>
_stateFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"ContactManager.state"
</code>
how to connect to the sql database or .mdf file rather than a binary file.
Experts I have a question---how to connect the following project to sql server and user insert/edit/update queries to the database-
1.Please open the page http://www.informit.com/store/produc...sbn=0672329859
2.click on Downloads
3.Please download the Contact Manager
4.Unzip ContactManager.zip
5.Please open ContactRepository.cs file
6.There you will find the following code -
using System;
-
using System.Collections.Generic;
-
using System.IO;
-
using System.Linq;
-
using System.Runtime.Serialization.Formatters.Binary;
-
-
namespace ContactManager.Model
-
{
-
public class ContactRepository
-
{
-
private List<Contact> _contactStore;
-
private readonly string _stateFile;
-
-
public ContactRepository()
-
{
-
_stateFile = Path.Combine(
-
AppDomain.CurrentDomain.BaseDirectory,
-
"ContactManager.state"
-
);
-
-
Deserialize();
-
}
-
-
public void Save(Contact contact)
-
{
-
if (contact.Id == Guid.Empty)
-
contact.Id = Guid.NewGuid();
-
-
if (!_contactStore.Contains(contact))
-
_contactStore.Add(contact);
-
-
Serialize();
-
}
-
-
public void Delete(Contact contact)
-
{
-
_contactStore.Remove(contact);
-
Serialize();
-
}
-
-
public List<Contact> FindByLookup(string lookupName)
-
{
-
IEnumerable<Contact> found =
-
from c in _contactStore
-
where c.LookupName.StartsWith(
-
lookupName,
-
StringComparison.OrdinalIgnoreCase
-
)
-
select c;
-
-
return found.ToList();
-
}
-
-
public List<Contact> FindAll()
-
{
-
return new List<Contact>(_contactStore);
-
}
-
-
private void Serialize()
-
{
-
using (FileStream stream =
-
File.Open(_stateFile, FileMode.OpenOrCreate))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
formatter.Serialize(stream, _contactStore);
-
}
-
}
-
-
private void Deserialize()
-
{
-
if (File.Exists(_stateFile))
-
{
-
using (FileStream stream =
-
File.Open(_stateFile, FileMode.Open))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
-
_contactStore =
-
(List<Contact>)formatter.Deserialize(stream);
-
}
-
}
-
else _contactStore = new List<Contact>();
-
}
-
}
-
}
-
-
7.Now here you are seeing the following
<code> - _stateFile = Path.Combine(
-
AppDomain.CurrentDomain.BaseDirectory,
-
"ContactManager.state"
</code>
8.What this project is doing is it is storing the data in a binary data file ContactManager.state
9.Then passing those values to the variable _stateFile
10. Now my question is how to connect this project to the sql server and in place of the binary data file I want a table to be user in sqlserver .
11.How that is to be done I do not know.
12.I know the basic data connection to the winform application and insert/update/delete operation
as followes
<code> - private void button1_Click(object sender, EventArgs e)
-
{
-
SqlConnection con = new SqlConnection("Data Source=STUDENT-1D40FC5\\SQLEXPRESS; Initial Catalog=LibraryMgnt;Integrated Security=SSPI");
-
string query = "insert into BookDetails(Title,Author,Publisher,Subject,No_Of_Copies)values('" + title.Text + "','" + author.Text + "','" + textBox3.Text + "','" + subject.Text + "','" + no.Text + "')";
-
SqlCommand cmd = new SqlCommand(query, con);
-
con.Open();
-
SqlDataReader reader = cmd.ExecuteReader();
-
MessageBox.Show("The Book Details are saved.");
-
while (reader.Read())
-
{
-
}
-
reader.Close();
-
-
}
</code>
13.But in this project how that connection is to be established I do not know and also how insert/update/delete queries are to be used .
14.Experts I thought that ContactRepository.cs file only is responsible for the data connection if I am wrong then please see the whole project where the required changes are to be made.That is the main reason I have send the url of the code project .
I am totally new to WPF please explain step by step
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
I know I have said this in a few of your posts now.
The tag for code is [code] not <code>
Stop making other people do this for you. It's lazy.
I have to say, you are asking an aweful lot of people just to answer
How to connect the following project to sql server and user insert/edit/update queries to the database
There really isn't a need to download your entire project etc. to tell you how to connect to a database and use insert/edit/update queries.
Database How-to parts 1 and 2 Database tutorial Part 1 Database tutorial Part 2
Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged
can you please suggest me how to replace the binary data file and what to do there as here binary data file is used
What do you mean by "replace"?
Update the value in the database?
Or replace the use of a binary file with some other kind of data: Something different than binary file?
If you mean 'update the database'... That is very well covered in the tutorials. Did you *do* the tutorials?
tlhintoq ,
You have got it .In the project they have used the binary data file .
I want to use the sql server and a table to store all the data .
tlhintoq the queries of all the insert/update/delete are to be operated on that table.
I know the queries.
Database connection also i know .
But the problem is if you see in Address.cs there are
private string _city;
private string _country;
private string _line1;
private string _line2;
private string _state;
private string _zip;
these properties.
Also in Contact.cs there are these following properties
private Address _address = new Address();
private string _cellPhone;
private string _firstName;
private string _homePhone;
private Guid _id = Guid.Empty;
private string _imagePath;
private string _jobTitle;
private string _lastName;
private string _officePhone;
private string _organization;
private string _primaryEmail;
private string _secondaryEmail;
Now I have to pass them to the table sql server.
In the file ContactRepository.cs
there is
private List<Contact> _contactStore;
private readonly string _stateFile;
So that means that in the list _contactStore the all properties of contact are passed
And in the following code
public ContactRepository()
{
_stateFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"ContactManager.state"
);
Deserialize();
}
The binary file is getting the values.
Now what I have to do ...Please tell me.
Please do not get angry if I am asking some silly questions(It is because I am fresher)
Please help
I will ask you again... Did you bother to walk through the two tutorials?
Just do the tutorials from start to finish. Don't try to apply them to your problem right now. First learn on a small scale how to interact with a database. Learn how to update small projects, small tables. Learn how to do the queries.
*THEN* take that new understanding and try to apply it to your larger project.
First you asked for help on how to do an update. Then you responded that you know how to update/query/connect/etc.
I know the queries.
Database connection also i know.
Then you say the problem is that you have properties. Why are properties such as a name or address a problem? Isn't that the sort of data you are wanting update?
Without asking others to download your entire project and do all your work for you... what exactly is the problem? Do you need help updating a database? Do you need help serializing a class?
Hello Experts,
i m also a part in this project ...
Here is the proper question .... i hope you guys can help us in this issue ...
The actual WPF project code that we have using this concept :
Taking data from a binary file and utilizing it in project .
What we want to do is ... instead of this binary file we want to take data from MSSQL database .
Here i m able to do mssql connection and i get my database values in to this application .
The Problem was , the data has to be formatted in a list type of way , so that we can utilize ...
See the following code ... -
private void Deserialize()
-
{
-
if (File.Exists(_stateFile))
-
{
-
using (FileStream stream =
-
File.Open(_stateFile, FileMode.Open))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
-
_contactStore =
-
(List<Contact>)formatter.Deserialize(stream);
-
}
-
}
-
else _contactStore = new List<Contact>();
-
}
-
this is the function which takes binary data from file and passing it to application ...
this is the following line doing that specific operation on above function ... -
_contactStore =
-
(List<Contact>)formatter.Deserialize(stream);
-
so i have to alter this piece of code to replace my mssql data instead of this binary data .
i get my mssql data in some string type variables . now i have to store those values to _contactStore object , so that application can use that . Here comes the problem . this _contact Store object requires list type of input . so i assembled my mssql values into list type and i tried to pass there ... but it is not accepting that .....
this is the actual problem what we are having .... can any one help us on this ...
this _contact Store object requires list type of input .
Does it? _contactstore is a List<> of Contact objects. - private List<Contact> _contactStore;
There is nothing here that tells us what data is *in* a Contact. Maybe it also contains one or more List<> variables. Maybe it doesn't. If you say a Contact object requires a List<> type as its input then we have to beleive you, but its seems unlikely. I would expect a Contact object to have a lot of properties such as - _FirstName
- _LastName
- _StreetAddress
- _City
and so on.
Regardless what it contains you need to make a new Contact object and .Add it to the List<> http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Sir I think you have not yet seen the project.I am sure if you see it once you can give the exact solution. ---It is my earnest request to you if you download the project from http://www.informit.com/store/produc...sbn=0672329859
I know you are too busy , but it will be your kind benevolence if you once glance upon it . It won't take more than 2 sec to download sir.
Sir the Contact.cs is as follows
And the Address.cs is as followes-- -
using System;
-
-
namespace ContactManager.Model
-
{
-
[Serializable]
-
public class Address : Notifier
-
{
-
private string _city;
-
private string _country;
-
private string _line1;
-
private string _line2;
-
private string _state;
-
private string _zip;
-
-
public string City
-
{
-
get { return _city; }
-
set
-
{
-
_city = value;
-
OnPropertyChanged("City");
-
}
-
}
-
-
public string Country
-
{
-
get { return _country; }
-
set
-
{
-
_country = value;
-
OnPropertyChanged("Country");
-
}
-
}
-
-
public string Line1
-
{
-
get { return _line1; }
-
set
-
{
-
_line1 = value;
-
OnPropertyChanged("Line1");
-
}
-
}
-
-
public string Line2
-
{
-
get { return _line2; }
-
set
-
{
-
_line2 = value;
-
OnPropertyChanged("Line2");
-
}
-
}
-
-
public string State
-
{
-
get { return _state; }
-
set
-
{
-
_state = value;
-
OnPropertyChanged("State");
-
}
-
}
-
-
public string Zip
-
{
-
get { return _zip; }
-
set
-
{
-
_zip = value;
-
OnPropertyChanged("Zip");
-
}
-
}
-
}
-
}
-
Sir I think you have not yet seen the project.I am sure if you see it once you can give the exact solution. ---It is my earnest request to you if you
You're right. I haven't downloaded your project, nor am I going to. Someone else with more free time might. But frankly, I don't make it a habit to download unknown code and run it on my PC. Nor do I make it a habit to completely debug other people's projects.
You provided the code for your Contact class. As I thought, it does not have a List<> in it, but a bunch of properties as one would suspect. Which means your original statement that it requires a List<> as input is wrong.
this _contact Store object requires list type of input .
i get my mssql data in some string type variables . now i have to store those values to _contactStore object
Stop right there. _contactstore is not an object. It is a list of objects. It is a List<> of Contact objects. A List<> is a fancy array if that helps. So _contactstore is an array of Contact objects.
If you are getting your mssql data as a bunch of strings that's great. Make a new Contact with them since your Contact object is also a bunch of string properties.
- public class Contact : Notifier
-
{
-
private Address _address = new Address();
-
private string _cellPhone;
-
private string _firstName;
-
private string _homePhone;
-
private Guid _id = Guid.Empty;
-
private string _imagePath;
-
private string _jobTitle;
-
private string _lastName;
-
private string _officePhone;
-
private string _organization;
-
private string _primaryEmail;
-
private string _secondaryEmail;
Sir
I have failed to send the data to the
_contactStore
in the
_contactStore =
(List<Contact>)formatter.Deserialize(stream);
Can you please tell me in which format the data has to be send so that _contactStore will accept the data and subsequently that can be stored in table in sql
I am sorry to say I have never worked on the "storing list of objects which can be passed to the table in database in sql server"
Just give me an example or refer a url so that I can come to know that .
It will be your kind benevolence if you suggest.
From your own code:
_contactStore is a List of Contact objects. - _contactStore = new List<Contact>();
And a Contact has a bunch of public properties that make sense for a Contact: - public string ImagePath
-
{
-
get { return _imagePath; }
-
set
-
{
-
_imagePath = value;
-
OnPropertyChanged("ImagePath");
-
}
-
}
-
-
public string FirstName
-
{
-
get { return _firstName; }
-
set
-
{
-
_firstName = value;
-
OnPropertyChanged("FirstName");
-
OnPropertyChanged("LookupName");
-
}
-
}
-
-
// and so on with all the other properties of a Contact
Can you please tell me in which format the data has to be send so that _contactStore will accept the data
You have to send your data to the Contact object the way it is defined. EX: For the FirstName send it a string as defined in line 11 of the code above.
Just give me an example or refer a url so that I can come to know that .
This isn't the complicated part. This is a basic property. http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
Please don't be offended when I say "If you're having this much trouble in such basic areas then this project may be beyond what you/your company can do."
Maybe it's time to tell your boss that you don't have the training to complete this project. It might be the nudge (s)he needs to get you some more training which can only further your career.
Either that or your company needs to quit underbidding qualified coders on projects that they don't have the expertise to handle - because it is unfair to their staff (you) to expect results they aren't prepared to produce. Nobody needs that kind of pressure placed upon them. It just makes people miserable.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Refky Wahib |
last post by:
Hi
I need Technical Support
I finished a Great project using .Net and SQL Server and .Net Mobile
Control
My Business case is to implement this Program to accept about 1
Million concurrent...
|
by: ebrahimbandookwala |
last post by:
Hi , I am trying to connect to MS Sql server 2000 from Java (1.4.2 /
1.5 ). I installed my Sql Server(8.00.382) from the one supplied with
VS.NET 2001. When I installed it on my laptop it did not...
|
by: Lauren Quantrell |
last post by:
I'm wondering which is the best approach using an Access2K front end
and a SQL Server 2K backend.
I have a stored procedure running three INSERT INTO statements that
inserts records into three...
|
by: Jim Devenish |
last post by:
I am continuing my exploration about upsizing to SQLServer from Access
2000.
I have a split database with a front-end and a back-end, each of which
is A2K. I have spent some time in bookshops...
|
by: Dave Dudley |
last post by:
Hi,
I have a new ASP .Net project that has been developed on our
development machine and connects to SQL Server 2000 on the same
machine. It is connecting via a connection string similiar to:...
|
by: Galina Grechka |
last post by:
Hi
Recently I started get error message every time when trying to open web
project from Microsoft Visual Studio.Net:
Unable to open Web project. The path does not correspond to URL. The two...
|
by: Dan Sikorsky |
last post by:
Can I use SQLServer 2000 with ASP.NET 2.0 instead of SQLServer 2005, and use
the .Net 2.0 Membership functionality?
I've setup my Login page, controls, etc., and now it's time to use the Web...
|
by: CriticalJ |
last post by:
I have several projects that have been working for at least four
years. Last Thursday, the connections for these projects began
dropping immediately upon opening the project. When I test the...
|
by: adjo |
last post by:
I am working on an app with an Access2002 frontend and Sql2005
backend. I have to use integrated security. I want to prevent my users
from altering data in another way than via the frontend.
It...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |