Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL
Hi!
 
Im kind of a beginner to C# and programming in genreal and in order to get better at it i created a forms application that registers customers.
 
At first i did this by trying to learn about OOP and now im going to the DB stuff.
 
I want to insert info to the database but i get the following:
 
"invalid instance"
 
The code for inserting to Db looks like this:
 
 
//String ConnectionString = ;
        SqlConnection Cn = new SqlConnection(@"Data Source=ADMIN-PC\\MasterDB;Initial Catalog=Test;Integrated Security=True");
        SqlCommand Command = new SqlCommand();
        SqlDataReader DataReader;
 
public void InsertInfo(string _valueOne, string _ValueTwo, string _valueThree, string _valuefour, string valueFive, string valuesix, string valueseven,
           string valueeight, string valuenine, string valueten)
       {
           try
           {
 
               Cn.Open();
               Command.Connection = Cn;
 
               Command.CommandText = @"INSERT INTO CustomerInfo(Firstname,LastName,HoneNr,WorkNr,BusinessMail,PrivateMail,Street,City,Zipcode,Country)
           @VALUES('" + _valueOne + "', '" + _ValueTwo + "','" + _valueThree + "','" + _valuefour + "','" + valueFive + "','" + valuesix + "', '" + valueseven + "','" + valueeight + "','" + valuenine + "','" + valueten + "')";
 
               Command.ExecuteNonQuery();
               Cn.Close();
               MessageBox.Show("infor inserted");
           }
 
           catch (Exception ObjException)
           {
               throw ObjException;
           }
}
 
As soon as it comes to cn.open() it crashes and i don´t really understand what the problem is?
 
Also i want to get data from the Db and add this to a gridview
 
The code so far for this is like this:
 
public void GetInfo()
{
    Cn.Open();
    Command.CommandText = "SELECT * FROM CustomerInfo";
    DataReader = Command.ExecuteReader();
 
    if (DataReader.HasRows)
    {
        while (DataReader.Read())
        {
 
        }
    }
}
 
Im kind of stuck here since i don´t know how to return the data, should i return it in a list? Also how do i add this to the gridview?
 
im a beginner and ive been at this for a few hours so if this is really simple for you be patient with me ill get there!
 
thanks!
Posted 18-Nov-12 8:56am
Edited 18-Nov-12 8:59am
v2
Comments
ryanb31 at 19-Nov-12 10:05am
   
The error "invalid instance" is telling you that the SQL connection is not valid. Apparently, there is no SQL instance on the server "Admin-PC" named MasterDB. My guess is you are using the default SQL instance so just remove \MasterDB from the connection string. You can look in the Services list (Admin Tools) and see the name of the SQL instance.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is a horrible idea. As a beginner, you should learn C# on the console, then winforms and then perhaps start to worry about learning SQL and so on. You're biting off more than you can chew, and it's unlikely you'll learn much as a result. Having said that, this site is not just about questions, it has a rich library of articles and if you were to search those, you'd find all the answers you need.
 
Ideally you'd return your data as a collection of a strongly typed object, but really, all your data code is poorly designed, simply because, again, you're trying to learn too many things at once. Learn to do one thing reasonably well, then start on the next. Don't try to learn it all at once.
  Permalink  
Comments
ridoy at 18-Nov-12 15:42pm
   
+5
Nelek at 18-Nov-12 16:48pm
   
OP's comment to you moved from a non-solution below
Ive done console applications, ive don forms applications and im at this stage where Ive learned sql and going forward with this.

Please if you wont help me with a solution dont reply!
Sergey Alexandrovich Kryukov at 18-Nov-12 18:42pm
   
Maybe this answer is not the most helpful, but I reported OP for abuse, because "if you... don't reply" is simply rude. OP does not like the answer and responds with "shut up" -- this is what it, is in a nutshell.
--SA
ryanb31 at 19-Nov-12 10:03am
   
OP is right. If you don't have an answer, don't post a solution. If you just have a comment, then post a comment.
Sergey Alexandrovich Kryukov at 19-Nov-12 11:21am
   
I understand that, and I did not post an answer this time, бут I still think this is rude... this answer may be not the best, but it conducts helpful idea, and, come to think about, to the point.
--SA
ryanb31 at 19-Nov-12 10:03am
   
"Please if you wont help me with a solution dont reply!" Well said.

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



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100