Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# database WebService
I have created database name Notes.Table name tbNotes. Column names are NotesID(int),
Notes(Varchar), Client(Varchar),Date(DateTime),Category(Varchar).
 
Here is my Code for webservices....
 
[WebMethod]
public int AddNotes(int NotesID, string Notes,string Client, string Date, string Category)
{
 
string strConn="Data Source=#####; Initial Catalog=Notes;uid=sa; pwd=####;";
SqlConnection cnn = new SqlConnection(strConn);
cnn.Open();
 
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
 
cmd.CommandText =
 
"insert into tbNotes(NotesID,Notes,Client,Data,Category)values (" + NotesID + ",'" + Notes + "','" + Client + "','" + Date + "', " + Category + ")";
int i = cmd.ExecuteNonQuery();
cnn.Close();
 
return i;
}
private void button1_Click(object sender, EventArgs e)
{
 
if (txtnotesid.Text == "" || txtnotes.Text == "" || txtclient.Text == "" || category.Text == "")
{
MessageBox.Show("Please Enter The Details");
}
 
else
{
notesapp.WebService1 obj = new notesapp.WebService1();
}
Posted 7-Dec-12 22:39pm
Edited 7-Dec-12 23:12pm
v3
Comments
Krunal Rohit at 8-Dec-12 3:44am
   
and your problem is........?????????
Ritesh Zaveri at 8-Dec-12 3:53am
   
after adding details in form and then click on save button, my details should be stored in database using web services method
Krunal Rohit at 8-Dec-12 4:02am
   
Okay, so where's your Save Button code ???
Ritesh Zaveri at 8-Dec-12 4:11am
   
Save Button Code is in window form file...
 
private void button1_Click(object sender, EventArgs e)
{
 
if (txtnotesid.Text == "" || txtnotes.Text == "" || txtclient.Text == "" || category.Text == "")
{
MessageBox.Show("Please Enter The Details");
}
 
else
{
notesapp.WebService1 obj = new notesapp.WebService1();
}
Ritesh Zaveri at 8-Dec-12 4:30am
   
Thanks...:)
Krunal Rohit at 8-Dec-12 4:32am
   
Cheers mate. :) All the best :) :)
Ritesh Zaveri at 8-Dec-12 4:35am
   
Whats is StatuLabel.Text??
Krunal Rohit at 8-Dec-12 4:38am
   
that's optional... ExecuteNonQuery returns no of rows affected, so I've displayed over there..
Ritesh Zaveri at 8-Dec-12 5:03am
   
I have written parameters like this in Window form :: objService.AddNotes(txtnotesid.Text,txtnotes.Text,txtclient.Text,datetime.Text,category.Text);
and in webservice method my parameteres are like this :: public int AddNotes(string NotesID, string Notes, string Client, string Date, string Category)....
Ritesh Zaveri at 8-Dec-12 5:04am
   
But Getting Error like
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: The name "Marketing" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
Ritesh Zaveri at 8-Dec-12 5:05am
   
Marketing is a item in Category... Category contains combobox having items like Marketing,Sales,General,Operational,Payment.
Krunal Rohit at 8-Dec-12 5:09am
   
ComboBox.SelectedItem.ToString();
Ritesh Zaveri at 8-Dec-12 5:14am
   
Still getting same error as above Soap Handled Exception::
The name "Marketing" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Krunal Rohit at 8-Dec-12 5:17am
   
Category is your table column name... Make changes as per this
Ritesh Zaveri at 8-Dec-12 5:19am
   
What changes??
Krunal Rohit at 8-Dec-12 5:45am
   
Column name is not permitted and its seems like you're passing Column name.. So accordingly make changes.. ;)
Ritesh Zaveri at 8-Dec-12 6:04am
   
Thanks...:) Now it works..:)
Krunal Rohit at 8-Dec-12 6:08am
   
Good Day :)

1 solution

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

Solution 1

private void button1_Click(object sender, EventArgs e)
{
 
if (txtnotesid.Text == "" || txtnotes.Text == "" || txtclient.Text == "" || category.Text == "")
{
MessageBox.Show("Please Enter The Details");
}
 
else
{
notesapp.WebService1 obj = new notesapp.WebService1();
int n = obj.AddNotes(txtnotesid.Text,txtnotes.Text,txtclient.Text,category.Text);
StatuLabel.Text = n.ToString();
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 279
1 OriginalGriff 190
2 DamithSL 185
3 Peter Leow 105
4 Kornfeld Eliyahu Peter 105
0 OriginalGriff 7,355
1 DamithSL 5,254
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 8 Dec 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